/** axel/quote 契约（阶段0 录证结论，阶段1 runtime 白名单） */

export const AXEL_QUOTE_URL_FRAGMENT = "services.mothership.com/axel/quote";

export function isAxelQuoteResponseUrl(url: string): boolean {
  return url.toLowerCase().includes("axel/quote");
}

function getAt(obj: unknown, ...keys: string[]): unknown {
  let cur = obj;
  for (const key of keys) {
    if (cur == null || typeof cur !== "object" || Array.isArray(cur)) {
      return undefined;
    }
    cur = (cur as Record<string, unknown>)[key];
  }
  return cur;
}

/** guaranteed 分支：lowest 缺失时 fallback bestValue（录证 2026-06） */
export function resolveGuaranteedLowestTier(
  guaranteed: unknown,
): unknown {
  if (guaranteed == null || typeof guaranteed !== "object" || Array.isArray(guaranteed)) {
    return undefined;
  }
  const lowest = getAt(guaranteed, "lowest");
  if (lowest != null) {
    return lowest;
  }
  return getAt(guaranteed, "bestValue");
}
