/**
 * Operator carrier icon mapping per TZ §4.1.22.
 *
 * Maps an IATA carrier code to locale-aware icon asset paths served from
 * /assets/img/airlines-logo/. When `OperatingBy` is null (no operating
 * carrier from SSM/ASM/AOMM sources), a 7-range flight-number lookup
 * (SU-numbered flights only) resolves the implied carrier per the TZ table.
 *
 * @see /tmp/ri_tz_extract/content.txt lines 2985-3050
 */

export interface OperatorIconPaths {
  /** Wide rectangular logo — default for list rows and cards. */
  en: string;
  /** Russian locale variant (wide). Falls back to `en` when absent. */
  ru?: string;
  /** Small round badge — used in multi-leg rows and the mini-list. */
  round?: string;
}

/**
 * 35-carrier lookup table (TZ §4.1.22, Таблица 67).
 * F7 is an internal alias mapped to Aeroflot assets (not in TZ table but
 * present in Angular's _flight.scss — retained for parity).
 */
export const OPERATOR_LOGO_PATHS: Readonly<Record<string, OperatorIconPaths>> =
  Object.freeze({
    SU: {
      en: "/assets/img/airlines-logo/aeroflot/large/en.png",
      ru: "/assets/img/airlines-logo/aeroflot/large/ru.png",
      round: "/assets/img/airlines-logo/aeroflot/round.png",
    },
    /** Aeroflot alias (Angular parity, not in TZ table). */
    F7: {
      en: "/assets/img/airlines-logo/aeroflot/large/en.png",
      ru: "/assets/img/airlines-logo/aeroflot/large/ru.png",
      round: "/assets/img/airlines-logo/aeroflot/round.png",
    },
    HZ: {
      en: "/assets/img/airlines-logo/aurora/large/en.svg",
      ru: "/assets/img/airlines-logo/aurora/large/ru.svg",
      round: "/assets/img/airlines-logo/aurora/round.png",
    },
    FV: {
      en: "/assets/img/airlines-logo/rossiya/large/en.svg",
      ru: "/assets/img/airlines-logo/rossiya/large/ru.svg",
      round: "/assets/img/airlines-logo/rossiya/round.png",
    },
    RO: {
      en: "/assets/img/airlines-logo/tarom/large.png",
      round: "/assets/img/airlines-logo/tarom/round.svg",
    },
    DP: {
      en: "/assets/img/airlines-logo/pobeda/large.svg",
      round: "/assets/img/airlines-logo/pobeda/round.png",
    },
    OM: {
      en: "/assets/img/airlines-logo/miat/large.svg",
      round: "/assets/img/airlines-logo/miat/round.svg",
    },
    KL: {
      en: "/assets/img/airlines-logo/klm/large.png",
      round: "/assets/img/airlines-logo/klm/round.png",
    },
    AY: {
      en: "/assets/img/airlines-logo/finnair/large.svg",
      round: "/assets/img/airlines-logo/finnair/round.png",
    },
    DL: {
      en: "/assets/img/airlines-logo/delta/large.svg",
      round: "/assets/img/airlines-logo/delta/round.png",
    },
    OK: {
      en: "/assets/img/airlines-logo/czech-airline/large.png",
      round: "/assets/img/airlines-logo/czech-airline/round.svg",
    },
    JU: {
      en: "/assets/img/airlines-logo/air-serbia/large.svg",
      round: "/assets/img/airlines-logo/air-serbia/round.svg",
    },
    UX: {
      en: "/assets/img/airlines-logo/air-europa/large.svg",
      round: "/assets/img/airlines-logo/air-europa/round.svg",
    },
    BT: {
      en: "/assets/img/airlines-logo/air-baltic/large.svg",
      round: "/assets/img/airlines-logo/air-baltic/round.svg",
    },
    AM: {
      en: "/assets/img/airlines-logo/aeromexico/large.svg",
      round: "/assets/img/airlines-logo/aeromexico/round.svg",
    },
    AR: {
      en: "/assets/img/airlines-logo/aerolineas-argentinas/large.png",
      round: "/assets/img/airlines-logo/aerolineas-argentinas/round.svg",
    },
    KM: { en: "/assets/img/airlines-logo/airmalta/large.svg" },
    AF: { en: "/assets/img/airlines-logo/airfrance/large.svg" },
    AZ: { en: "/assets/img/airlines-logo/alitalia/large.svg" },
    PG: { en: "/assets/img/airlines-logo/bangkok-airways/large.png" },
    SN: { en: "/assets/img/airlines-logo/brussels-airlines/large.png" },
    FB: { en: "/assets/img/airlines-logo/bulgaria-air/large.png" },
    CI: { en: "/assets/img/airlines-logo/china-airlines/large.png" },
    MU: { en: "/assets/img/airlines-logo/china-eastern/large.svg" },
    CZ: { en: "/assets/img/airlines-logo/china-southern/large.svg" },
    GA: { en: "/assets/img/airlines-logo/garuda-indonesia/large.png" },
    FI: { en: "/assets/img/airlines-logo/icelandair/large.svg" },
    KO: { en: "/assets/img/airlines-logo/kenya-airways/large.svg" },
    KE: { en: "/assets/img/airlines-logo/korean-air/large.svg" },
    JL: { en: "/assets/img/airlines-logo/japan-airlines/large.svg" },
    LO: { en: "/assets/img/airlines-logo/polish-airlines/large.png" },
    ME: { en: "/assets/img/airlines-logo/mea/large.png" },
    S7: { en: "/assets/img/airlines-logo/s7/large.svg" },
    SV: { en: "/assets/img/airlines-logo/saudi-arabian-airlines/large.png" },
    MF: { en: "/assets/img/airlines-logo/xiamen-airlines/large.png" },
    VN: { en: "/assets/img/airlines-logo/vietnam-airlines/large.png" },
  });

/**
 * When `OperatingBy` is null, the SU flight number determines the implied
 * carrier per TZ §4.1.22 range table. Returns `null` for ranges that
 * explicitly show no icon (SU3000-4999, SU5800-5999).
 *
 * Range carrier codes:
 *   SU1–2999    → SU
 *   SU3000–4999 → null  (no icon)
 *   SU5000–5399 → DP (Pobeda)
 *   SU5400–5799 → HZ (Aurora)
 *   SU5800–5999 → null  (no icon)
 *   SU6000–6999 → FV (Rossiya)
 *   SU7000–9999 → SU
 */
export function resolveCarrierByFlightNumber(
  flightNumber: string | number,
): string | null {
  const raw = String(flightNumber).replace(/\D/g, "");
  const n = parseInt(raw, 10);
  if (Number.isNaN(n)) return null;
  if (n >= 1 && n <= 2999) return "SU";
  if (n >= 3000 && n <= 4999) return null;
  if (n >= 5000 && n <= 5399) return "DP";
  if (n >= 5400 && n <= 5799) return "HZ";
  if (n >= 5800 && n <= 5999) return null;
  if (n >= 6000 && n <= 6999) return "FV";
  if (n >= 7000 && n <= 9999) return "SU";
  return null;
}

/**
 * Resolve the display paths for a given carrier code.
 * Returns `null` when:
 * - The carrier is explicitly unknown (not in the 35-carrier table), or
 * - A range-based fallback resolves to "no icon" (SU3000-4999, SU5800-5999).
 *
 * Callers should hide the logo element entirely when `null` is returned.
 */
export function resolveOperatorLogoPaths(
  carrierCode: string | null | undefined,
): OperatorIconPaths | null {
  if (!carrierCode) return null;
  return OPERATOR_LOGO_PATHS[carrierCode] ?? null;
}

/**
 * Pick the concrete asset src for a given set of paths + render options.
 *
 * @param paths   Resolved paths from `resolveOperatorLogoPaths`.
 * @param locale  BCP-47 or 2-char language code ("ru", "ru-RU", "en").
 * @param round   When true, prefer the round badge variant.
 */
export function pickLogoSrc(
  paths: OperatorIconPaths,
  locale: string,
  round: boolean,
): string {
  const lang = locale.slice(0, 2).toLowerCase();
  if (round && paths.round) return paths.round;
  return lang === "ru" && paths.ru ? paths.ru : paths.en;
}
