/**
 * Country code normalization utilities
 *
 * Converts various country code formats and country names to ISO 3166-1 alpha-2 codes
 */

/**
 * Normalizes country codes and names to ISO 3166-1 alpha-2 format (2-letter codes)
 *
 * Supports:
 * - ISO 3166-1 alpha-2 codes (e.g., "US", "GB")
 * - ISO 3166-1 alpha-3 codes (e.g., "USA", "GBR")
 * - Common country names (e.g., "United States", "China")
 *
 * @param input - Country code or name to normalize
 * @returns ISO 3166-1 alpha-2 code or null if not recognized
 *
 * @example
 * normalizeCountryCode("USA") // "US"
 * normalizeCountryCode("United States") // "US"
 * normalizeCountryCode("US") // "US"
 * normalizeCountryCode("invalid") // null
 */
export function normalizeCountryCode(input: string): string | null {
  if (!input) return null;

  const s = input.trim();
  const upper = s.toUpperCase();

  // Already in ISO 3166-1 alpha-2 format
  if (/^[A-Z]{2}$/.test(upper)) return upper;

  // ISO 3166-1 alpha-3 to alpha-2 mapping
  const iso3to2: Record<string, string> = {
    USA: "US", GBR: "GB", UK: "GB", CHN: "CN", DEU: "DE", FRA: "FR", ITA: "IT", ESP: "ES",
    CAN: "CA", MEX: "MX", AUS: "AU", NZL: "NZ", JPN: "JP", KOR: "KR", PRK: "KP", TWN: "TW",
    VNM: "VN", THA: "TH", IDN: "ID", IND: "IN", BRA: "BR", ARG: "AR", CHL: "CL", PER: "PE",
    ZAF: "ZA", RUS: "RU", UKR: "UA", NLD: "NL", BEL: "BE", CHE: "CH", SWE: "SE", NOR: "NO",
    DNK: "DK", POL: "PL", AUT: "AT", PRT: "PT", GRC: "GR", CZE: "CZ", HUN: "HU", IRL: "IE",
    ISR: "IL", PAK: "PK", BGD: "BD", PHL: "PH", MYS: "MY", SGP: "SG", HKG: "HK", MAC: "MO"
  };

  if (iso3to2[upper]) return iso3to2[upper];

  // Country name to ISO 3166-1 alpha-2 mapping
  const nameMap: Record<string, string> = {
    "UNITED STATES": "US", "UNITED STATES OF AMERICA": "US", "AMERICA": "US", "U.S.": "US", "U.S.A": "US", "USA": "US",
    "UNITED KINGDOM": "GB", "UK": "GB", "GREAT BRITAIN": "GB", "ENGLAND": "GB",
    "CHINA": "CN", "PEOPLE'S REPUBLIC OF CHINA": "CN", "PRC": "CN",
    "CANADA": "CA", "MEXICO": "MX", "JAPAN": "JP", "SOUTH KOREA": "KR", "KOREA, REPUBLIC OF": "KR",
    "TAIWAN": "TW", "VIETNAM": "VN", "THAILAND": "TH", "INDONESIA": "ID", "INDIA": "IN",
    "GERMANY": "DE", "FRANCE": "FR", "ITALY": "IT", "SPAIN": "ES", "NETHERLANDS": "NL", "BELGIUM": "BE",
    "SWITZERLAND": "CH", "SWEDEN": "SE", "NORWAY": "NO", "DENMARK": "DK", "POLAND": "PL", "AUSTRIA": "AT",
    "PORTUGAL": "PT", "GREECE": "GR", "CZECH REPUBLIC": "CZ", "CZECHIA": "CZ", "HUNGARY": "HU", "IRELAND": "IE",
    "RUSSIA": "RU", "UKRAINE": "UA", "AUSTRALIA": "AU", "NEW ZEALAND": "NZ",
    "BRAZIL": "BR", "ARGENTINA": "AR", "CHILE": "CL", "PERU": "PE", "SOUTH AFRICA": "ZA",
    "ISRAEL": "IL", "PAKISTAN": "PK", "BANGLADESH": "BD", "PHILIPPINES": "PH", "MALAYSIA": "MY", "SINGAPORE": "SG",
    "HONG KONG": "HK", "MACAU": "MO"
  };

  // Normalize input: remove dots, trim, uppercase
  const normalizedName = s.replace(/\./g, "").trim().toUpperCase();
  if (nameMap[normalizedName]) return nameMap[normalizedName];

  return null;
}