// src/lib/gsc/country-iso3.ts — mapping ISO-3166 alpha-2 → alpha-3 (minuscule, format GSC).
const ISO3: Record<string, string> = {
  FR: "fra",
  BE: "bel",
  CH: "che",
  CA: "can",
  US: "usa",
  GB: "gbr",
  DE: "deu",
  ES: "esp",
  IT: "ita",
  NL: "nld",
  PT: "prt",
  LU: "lux",
  AT: "aut",
  IE: "irl",
  AU: "aus",
  NZ: "nzl",
  MA: "mar",
  TN: "tun",
  SN: "sen",
  CI: "civ",
  // Extended coverage so the [query, country] sync can recognise (and, when
  // unconfigured, drop) traffic from these markets — mirrors language.ts.
  MC: "mco",
  TG: "tgo",
  CM: "cmr",
  ML: "mli",
  BF: "bfa",
  NE: "ner",
  GA: "gab",
  CG: "cog",
  CD: "cod",
  DZ: "dza",
  ZA: "zaf",
  IN: "ind",
  NG: "nga",
  PH: "phl",
  SG: "sgp",
  MX: "mex",
  AR: "arg",
  CO: "col",
  CL: "chl",
  PE: "per",
  VE: "ven",
  EC: "ecu",
  GT: "gtm",
  CU: "cub",
  BR: "bra",
  AO: "ago",
  MZ: "moz",
  SM: "smr",
  SE: "swe",
  NO: "nor",
  DK: "dnk",
  FI: "fin",
  PL: "pol",
  RU: "rus",
  JP: "jpn",
  CN: "chn",
  TW: "twn",
  KR: "kor",
  TR: "tur",
  GR: "grc",
  VN: "vnm",
  TH: "tha",
  ID: "idn",
  CZ: "cze",
  HU: "hun",
  RO: "rou",
};

/** Code pays GSC (alpha-3 minuscule) pour un alpha-2 ; null si inconnu. */
export function alpha2ToIso3(alpha2: string): string | null {
  return ISO3[alpha2.toUpperCase()] ?? null;
}

// Reverse of ISO3, built once: alpha-3 (lowercase, GSC format) → alpha-2.
const ALPHA2_BY_ISO3: Record<string, string> = Object.fromEntries(
  Object.entries(ISO3).map(([alpha2, iso3]) => [iso3, alpha2]),
);

/** Alpha-2 pour un code pays GSC (alpha-3 minuscule) ; null si inconnu. */
export function iso3ToAlpha2(iso3: string): string | null {
  return ALPHA2_BY_ISO3[iso3.toLowerCase()] ?? null;
}
