type AirlineInfo = {
  name: string;
};

const ICAO_AIRLINES: Record<string, AirlineInfo> = {
  AAL: { name: "American Airlines" },
  AAR: { name: "Asiana Airlines" },
  AAY: { name: "Allegiant Air" },
  ABY: { name: "Air Arabia" },
  ACA: { name: "Air Canada" },
  AEA: { name: "Air Europa" },
  AEE: { name: "Aegean Airlines" },
  AFL: { name: "Aeroflot" },
  AFR: { name: "Air France" },
  AIC: { name: "Air India" },
  AIQ: { name: "AirAsia" },
  ASL: { name: "Air Serbia" },
  ANE: { name: "Air Nostrum" },
  AIJ: { name: "Interjet" },
  AJT: { name: "Amerijet" },
  ALK: { name: "SriLankan Airlines" },
  AMX: { name: "Aeroméxico" },
  ANA: { name: "All Nippon Airways" },
  ANZ: { name: "Air New Zealand" },
  ASA: { name: "Alaska Airlines" },
  AUA: { name: "Austrian Airlines" },
  AVA: { name: "Avianca" },
  ARG: { name: "Aerolíneas Argentinas" },
  AWE: { name: "US Airways" },
  AXM: { name: "AirAsia" },
  AXB: { name: "Air India Express" },
  AZA: { name: "Alitalia / ITA Airways" },
  AZU: { name: "Azul" },
  BAW: { name: "British Airways" },
  BEL: { name: "Brussels Airlines" },
  BER: { name: "Air Berlin" },
  BTI: { name: "Air Baltic" },
  CAL: { name: "China Airlines" },
  CCA: { name: "Air China" },
  CEB: { name: "Cebu Pacific" },
  CES: { name: "China Eastern" },
  CHH: { name: "Hainan Airlines" },
  CFG: { name: "Condor" },
  CLH: { name: "Lufthansa CityLine" },
  CMP: { name: "Copa Airlines" },
  CPA: { name: "Cathay Pacific" },
  CRK: { name: "Hong Kong Airlines" },
  CQH: { name: "Spring Airlines" },
  CSC: { name: "Sichuan Airlines" },
  CSN: { name: "China Southern" },
  CSZ: { name: "Shenzhen Airlines" },
  CTN: { name: "Croatia Airlines" },
  CXA: { name: "Xiamen Airlines" },
  DAH: { name: "Air Algerie" },
  DAL: { name: "Delta Air Lines" },
  DKH: { name: "Juneyao Airlines" },
  DAT: { name: "Brussels Airlines" },
  DLA: { name: "Air Dolomiti" },
  DLH: { name: "Lufthansa" },
  EIN: { name: "Aer Lingus" },
  ENY: { name: "Envoy Air" },
  EJU: { name: "easyJet Europe" },
  ELY: { name: "El Al" },
  ETD: { name: "Etihad Airways" },
  ETH: { name: "Ethiopian Airlines" },
  EVA: { name: "EVA Air" },
  EWG: { name: "Eurowings" },
  EZY: { name: "easyJet" },
  EXS: { name: "Jet2" },
  FFT: { name: "Frontier Airlines" },
  FDX: { name: "FedEx Express" },
  FDB: { name: "flydubai" },
  FIN: { name: "Finnair" },
  FJI: { name: "Fiji Airways" },
  GAF: { name: "German Air Force" },
  GFA: { name: "Gulf Air" },
  GIA: { name: "Garuda Indonesia" },
  GLO: { name: "GOL" },
  GTI: { name: "Atlas Air" },
  HAL: { name: "Hawaiian Airlines" },
  HKE: { name: "Hong Kong Express" },
  HVN: { name: "Vietnam Airlines" },
  IGO: { name: "IndiGo" },
  IBE: { name: "Iberia" },
  IBK: { name: "Norwegian Air Int'l" },
  IBB: { name: "Binter Canarias" },
  IBU: { name: "IndiGo" },
  ICE: { name: "Icelandair" },
  IBS: { name: "Iberia Express" },
  JAL: { name: "Japan Airlines" },
  JBU: { name: "JetBlue" },
  JJA: { name: "Jeju Air" },
  JJP: { name: "Jetstar" },
  JST: { name: "Jetstar" },
  JZA: { name: "Air Canada Jazz" },
  KAL: { name: "Korean Air" },
  KLM: { name: "KLM" },
  KZR: { name: "Air Astana" },
  LAN: { name: "LATAM Airlines" },
  LGL: { name: "Luxair" },
  LPE: { name: "LATAM Perú" },
  LOT: { name: "LOT Polish Airlines" },
  MAU: { name: "Air Mauritius" },
  MAS: { name: "Malaysia Airlines" },
  MSR: { name: "EgyptAir" },
  NAX: { name: "Norwegian Air Shuttle" },
  NKS: { name: "Spirit Airlines" },
  OMA: { name: "Oman Air" },
  OZW: { name: "SkyWest Airlines" },
  PAL: { name: "Philippine Airlines" },
  PIA: { name: "Pakistan Int'l Airlines" },
  PGT: { name: "Pegasus Airlines" },
  POE: { name: "Porter Airlines" },
  QFA: { name: "Qantas" },
  QTR: { name: "Qatar Airways" },
  RAM: { name: "Royal Air Maroc" },
  RJA: { name: "Royal Jordanian" },
  RPA: { name: "Republic Airways" },
  ROT: { name: "TAROM" },
  RYR: { name: "Ryanair" },
  SAS: { name: "Scandinavian Airlines" },
  SCO: { name: "Scoot" },
  SDM: { name: "Rossiya" },
  SCX: { name: "Sun Country Airlines" },
  SEJ: { name: "SpiceJet" },
  SEH: { name: "Sky Express" },
  SAA: { name: "South African Airways" },
  SIA: { name: "Singapore Airlines" },
  SKW: { name: "SkyWest Airlines" },
  SKY: { name: "Skymark Airlines" },
  SVA: { name: "Saudia" },
  SWA: { name: "Southwest Airlines" },
  SWR: { name: "Swiss Int'l Air Lines" },
  TAM: { name: "LATAM Brasil" },
  TAP: { name: "TAP Air Portugal" },
  THA: { name: "Thai Airways" },
  THY: { name: "Turkish Airlines" },
  TOM: { name: "TUI Airways" },
  TRA: { name: "Transavia" },
  TSC: { name: "Air Transat" },
  TWB: { name: "Tway Airlines" },
  TUI: { name: "TUI Airways" },
  TVF: { name: "Transavia France" },
  UAE: { name: "Emirates" },
  UAL: { name: "United Airlines" },
  USA: { name: "US Airways" },
  UPS: { name: "UPS Airlines" },
  VJC: { name: "VietJet Air" },
  VIR: { name: "Virgin Atlantic" },
  VOE: { name: "Volotea" },
  VOI: { name: "Volaris" },
  VOZ: { name: "Virgin Australia" },
  VLG: { name: "Vueling" },
  WJA: { name: "WestJet" },
  WIF: { name: "Widerøe" },
  WZZ: { name: "Wizz Air" },
  XAX: { name: "AirAsia X" },
};

export function lookupAirline(callsign: string | null): string | null {
  if (!callsign) return null;
  const trimmed = callsign.trim().toUpperCase();
  if (trimmed.length < 3) return null;
  const prefix = trimmed.slice(0, 3);
  return ICAO_AIRLINES[prefix]?.name ?? null;
}

export function parseFlightNumber(callsign: string | null): string | null {
  if (!callsign) return null;
  const trimmed = callsign.trim().toUpperCase();
  if (trimmed.length <= 3) return null;
  const digits = trimmed.slice(3).replace(/^0+/, "");
  if (!digits || !/^\d+[A-Z]?$/.test(digits)) return null;
  return digits;
}
