export const COUNTRY_NAME_BY_CODE: Record<string, string> = {
  AT: "Austria",
  BE: "Belgium",
  BG: "Bulgaria",
  CH: "Switzerland",
  CY: "Cyprus",
  CZ: "Czech Republic",
  DE: "Germany",
  DK: "Denmark",
  EE: "Estonia",
  ES: "Spain",
  FI: "Finland",
  FR: "France",
  GB: "United Kingdom",
  GR: "Greece",
  HR: "Croatia",
  HU: "Hungary",
  IE: "Ireland",
  IT: "Italy",
  LT: "Lithuania",
  LU: "Luxembourg",
  LV: "Latvia",
  MT: "Malta",
  NL: "Netherlands",
  NO: "Norway",
  PL: "Poland",
  PT: "Portugal",
  RO: "Romania",
  SE: "Sweden",
  SI: "Slovenia",
  SK: "Slovakia",
  UK: "United Kingdom",
  US: "United States",
};

export function normalizeCountryCode(code: string) {
  const normalized = code.trim().toUpperCase();
  if (normalized === "UK") return "GB";
  return normalized;
}

export function getCountryName(code: string) {
  const normalized = normalizeCountryCode(code);
  return COUNTRY_NAME_BY_CODE[normalized] ?? normalized;
}

export function getCountryFlag(code: string) {
  const normalized = normalizeCountryCode(code);
  if (!/^[A-Z]{2}$/.test(normalized)) return "";
  return String.fromCodePoint(...[...normalized].map((char) => 127397 + char.charCodeAt(0)));
}

export function formatCountryLabel(code: string) {
  const normalized = normalizeCountryCode(code);
  const name = getCountryName(normalized);
  const flag = getCountryFlag(normalized);
  return flag ? `${flag} ${name}` : name;
}
