type LicitayaStateInfo = {
  stateCode: string;
  stateName: string;
};

const LICITAYA_STATE_MAP: Record<string, LicitayaStateInfo> = {
  AGS: { stateCode: "1", stateName: "Aguascalientes" },
  BCN: { stateCode: "2", stateName: "Baja California" },
  BCS: { stateCode: "3", stateName: "Baja California Sur" },
  CAM: { stateCode: "4", stateName: "Campeche" },
  CHP: { stateCode: "5", stateName: "Chiapas" },
  CHH: { stateCode: "6", stateName: "Chihuahua" },
  COA: { stateCode: "7", stateName: "Coahuila" },
  COL: { stateCode: "8", stateName: "Colima" },
  CMX: { stateCode: "9", stateName: "Ciudad de Mexico" },
  CDMX: { stateCode: "9", stateName: "Ciudad de Mexico" },
  DUR: { stateCode: "10", stateName: "Durango" },
  GUA: { stateCode: "11", stateName: "Guanajuato" },
  GRO: { stateCode: "12", stateName: "Guerrero" },
  HID: { stateCode: "13", stateName: "Hidalgo" },
  JAL: { stateCode: "14", stateName: "Jalisco" },
  MEX: { stateCode: "15", stateName: "Estado de Mexico" },
  MIC: { stateCode: "16", stateName: "Michoacan" },
  MOR: { stateCode: "17", stateName: "Morelos" },
  NAY: { stateCode: "18", stateName: "Nayarit" },
  NLE: { stateCode: "19", stateName: "Nuevo Leon" },
  OAX: { stateCode: "20", stateName: "Oaxaca" },
  PUE: { stateCode: "21", stateName: "Puebla" },
  QUE: { stateCode: "22", stateName: "Queretaro" },
  ROO: { stateCode: "23", stateName: "Quintana Roo" },
  SLP: { stateCode: "24", stateName: "San Luis Potosi" },
  SIN: { stateCode: "25", stateName: "Sinaloa" },
  SON: { stateCode: "26", stateName: "Sonora" },
  TAB: { stateCode: "27", stateName: "Tabasco" },
  TAM: { stateCode: "28", stateName: "Tamaulipas" },
  TLA: { stateCode: "29", stateName: "Tlaxcala" },
  VER: { stateCode: "30", stateName: "Veracruz" },
  YUC: { stateCode: "31", stateName: "Yucatan" },
  ZAC: { stateCode: "32", stateName: "Zacatecas" },
};

function normalizeStateToken(value: string | null | undefined) {
  if (!value) {
    return null;
  }

  const normalized = value
    .toUpperCase()
    .replace(/[^A-Z]/g, "")
    .trim();

  return normalized.length > 0 ? normalized : null;
}

export function resolveLicitayaState(value: string | null | undefined): LicitayaStateInfo | null {
  const token = normalizeStateToken(value);

  if (!token) {
    return null;
  }

  return LICITAYA_STATE_MAP[token] ?? null;
}
