import { t } from '../locales';

const threeDigit = /^\d{3}$/;
const fourDigit = /^\d{4}$/;
const fiveDigit = /^\d{5}$/;
const sixDigit = /^\d{6}$/;

export const postalCodePatterns = {
  GB: /^GIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF|TN|TQ|TR|TS|TW|UB|W|WA|WC|WD|WF|WN|WR|WS|WV|YO|ZE)(\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}))|BFPO[ ]?\d{1,4}$/i,
  JE: /^JE\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/i,
  GG: /^GY\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/i,
  IM: /^IM\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}$/i,

  US: /^\d{5}([ -]\d{4})?$/,
  CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ ]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
  DE: /^\d{5}$/,
  JP: /^\d{3}-?\d{4}$/,
  FR: /^\d{2}[ ]?\d{3}$/,
  AU: fourDigit,
  IT: /^\d{5}$/,
  CH: fourDigit,
  AT: fourDigit,
  ES: /^\d{5}$/,
  NL: /^\d{4}[ ]?[A-Z]{2}$/i,
  BE: fourDigit,
  DK: fourDigit,
  SE: /^\d{3}[ ]?\d{2}$/,
  NO: fourDigit,
  BR: /^\d{5}-?\d{3}$/,
  PT: /^\d{4}(-\d{3})?$/,
  FI: fiveDigit,
  AX: /^22\d{3}$/,
  KR: /^\d{5}$/, // 2015年起使用5位数字邮编
  CN: /^\d{6}$/,
  TW: /^\d{3}(\d{2})?$/,
  SG: sixDigit,
  DZ: fiveDigit,
  AD: /^AD\d{3}$/,
  AR: /^([A-HJ-NP-Z])?\d{4}([A-Z]{3})?$/i,
  AM: /^(37)?\d{4}$/,
  AZ: /^\d{4}$/,
  BH: /^((1[0-2]|[2-9])\d{2})?$/,
  BD: fourDigit,
  BB: /^(BB\d{5})?$/i,
  BY: sixDigit,
  BM: /^[A-Z]{2}[ ]?[A-Z0-9]{2}$/i,
  BA: /^\d{5}$/,
  IO: /^BBND 1ZZ$/i,
  BN: /^[A-Z]{2}[ ]?\d{4}$/i,
  BG: fourDigit,
  KH: fiveDigit,
  CV: fourDigit,
  CL: /^\d{7}$/,
  CR: /^\d{4,5}|\d{3}-\d{4}$/,
  HR: /^\d{5}$/,
  CY: fourDigit,
  CZ: /^\d{3}[ ]?\d{2}$/,
  DO: fiveDigit,
  EC: /^([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?$/i,
  EG: fiveDigit,
  EE: fiveDigit,
  FO: threeDigit,
  GE: fourDigit,
  GR: /^\d{3}[ ]?\d{2}$/,
  GL: /^39\d{2}$/,
  GT: fiveDigit,
  HT: /^\d{4}$/,
  HN: /^(?:\d{5})?$/,
  HU: fourDigit,
  IS: threeDigit,
  IN: /^[1-9]\d{5}$/,
  ID: fiveDigit,
  IL: fiveDigit,
  JO: fiveDigit,
  KZ: sixDigit,
  KE: fiveDigit,
  KW: fiveDigit,
  LA: fiveDigit,
  LV: fourDigit,
  LB: /^(\d{4}([ ]?\d{4})?)?$/,
  LI: /^(948[5-9])|(949[0-7])$/,
  LT: fiveDigit,
  LU: fourDigit,
  MK: fourDigit,
  MY: fiveDigit,
  MV: fiveDigit,
  MT: /^[A-Z]{3}[ ]?\d{2,4}$/i,
  MU: /^(\d{3}[A-Z]{2}\d{3})?$/i,
  MX: fiveDigit,
  MD: fourDigit,
  MC: /^980\d{2}$/,
  MA: fiveDigit,
  NP: fiveDigit,
  NZ: fourDigit,
  NI: /^((\d{4}-)?\d{3}-\d{3}(-\d{1})?)?$/,
  NG: /^(\d{6})?$/,
  OM: /^(PC )?\d{3}$/i,
  PK: fiveDigit,
  PY: fourDigit,
  PH: fourDigit,
  PL: /^\d{2}-\d{3}$/,
  PR: /^00[679]\d{2}([ -]\d{4})?$/,
  RO: sixDigit,
  RU: sixDigit,
  SM: /^4789\d$/,
  SA: fiveDigit,
  SN: fiveDigit,
  SK: /^\d{3}[ ]?\d{2}$/,
  SI: fourDigit,
  ZA: fourDigit,
  LK: fiveDigit,
  TJ: sixDigit,
  TH: fiveDigit,
  TN: fourDigit,
  TR: fiveDigit,
  TM: sixDigit,
  UA: fiveDigit,
  UY: fiveDigit,
  UZ: sixDigit,
  VA: /^00120$/,
  VE: fourDigit,
  ZM: fiveDigit,

  // Territories & special regions
  AS: /^96799$/,
  CC: /^6799$/,
  CK: fourDigit,
  RS: sixDigit,
  ME: /^8\d{4}$/,
  CS: /^\d{5}$/, // historical
  YU: /^\d{5}$/, // historical
  CX: /^6798$/,
  ET: fourDigit,
  FK: /^FIQQ 1ZZ$/i,
  NF: /^2899$/,
  FM: /^(9694[1-4])([ -]\d{4})?$/,
  GF: /^9[78]3\d{2}$/,
  GN: threeDigit,
  GP: /^9[78][01]\d{2}$/,
  GS: /^SIQQ 1ZZ$/i,
  GU: /^969[123]\d([ -]\d{4})?$/,
  GW: fourDigit,
  HM: fourDigit,
  IQ: fiveDigit,
  KG: sixDigit,
  LR: fourDigit,
  LS: threeDigit,
  MG: threeDigit,
  MH: /^969[67]\d([ -]\d{4})?$/,
  MN: sixDigit,
  MP: /^9695[012]([ -]\d{4})?$/,
  MQ: /^9[78]2\d{2}$/,
  NC: /^988\d{2}$/,
  NE: fourDigit,
  VI: /^008(([0-4]\d)|(5[01]))([ -]\d{4})?$/,
  VN: sixDigit,
  PF: /^987\d{2}$/,
  PG: threeDigit,
  PM: /^9[78]5\d{2}$/,
  PN: /^PCRN 1ZZ$/i,
  PW: /^96940$/,
  RE: /^9[78]4\d{2}$/,
  SH: /^(ASCN|STHL) 1ZZ$/i,
  SJ: fourDigit,
  SO: fiveDigit,
  SZ: /^[HLMS]\d{3}$/i,
  TC: /^TKCA 1ZZ$/i,
  WF: /^986\d{2}$/,
  XK: fiveDigit,
  YT: /^976\d{2}$/,
};

export function validatePostalCode(postalCode: string, country?: string): boolean {
  if (!postalCode) return true;
  try {
    const countryUpper = country?.toUpperCase() as keyof typeof postalCodePatterns;
    if (!countryUpper) return false;
    return !postalCodePatterns[countryUpper] || postalCodePatterns[countryUpper].test(postalCode);
  } catch (error) {
    console.error(error);
    return false;
  }
}

export function getFieldValidation(fieldName: string, validations?: Record<string, any>, locale: string = 'en') {
  if (!validations || !validations[fieldName]) return {};
  const fieldValidation = validations[fieldName];
  const rules: Record<string, any> = {};
  if (fieldValidation.pattern) {
    rules.pattern = {
      value: new RegExp(fieldValidation.pattern),
      message: fieldValidation.pattern_message?.[locale] || t('payment.checkout.invalid', locale),
    };
  }
  return rules;
}
