import { parsePhoneNumberFromString } from 'libphonenumber-js';

// src/utils/phone.ts
var ISO3_TO_COUNTRY_CODE = {
  AGO: "AO",
  ARE: "AE",
  AUS: "AU",
  BDI: "BI",
  BRA: "BR",
  BWA: "BW",
  CAN: "CA",
  CHE: "CH",
  CHN: "CN",
  COD: "CD",
  EGY: "EG",
  ETH: "ET",
  GHA: "GH",
  GIN: "GN",
  GMB: "GM",
  GBR: "GB",
  HKG: "HK",
  IDN: "ID",
  IND: "IN",
  ISR: "IL",
  JPN: "JP",
  KEN: "KE",
  KOR: "KR",
  LBR: "LR",
  MUS: "MU",
  MEX: "MX",
  MOZ: "MZ",
  MWI: "MW",
  MYS: "MY",
  NAM: "NA",
  NGA: "NG",
  PHL: "PH",
  RWA: "RW",
  SAU: "SA",
  SGP: "SG",
  SLE: "SL",
  THA: "TH",
  TUR: "TR",
  TWN: "TW",
  TZA: "TZ",
  UGA: "UG",
  USA: "US",
  VNM: "VN",
  ZAF: "ZA",
  ZMB: "ZM"
};
var CURRENCY_TO_COUNTRY_CODE = {
  AED: "AE",
  AOA: "AO",
  AUD: "AU",
  BIF: "BI",
  BWP: "BW",
  BRL: "BR",
  CAD: "CA",
  CDF: "CD",
  CHF: "CH",
  CNY: "CN",
  EGP: "EG",
  ETB: "ET",
  GHS: "GH",
  GMD: "GM",
  GNF: "GN",
  GBP: "GB",
  HKD: "HK",
  IDR: "ID",
  ILS: "IL",
  INR: "IN",
  JPY: "JP",
  KES: "KE",
  KRW: "KR",
  LRD: "LR",
  MUR: "MU",
  MWK: "MW",
  MXN: "MX",
  MYR: "MY",
  MZN: "MZ",
  NAD: "NA",
  NGN: "NG",
  PHP: "PH",
  RWF: "RW",
  SAR: "SA",
  SGD: "SG",
  SLL: "SL",
  THB: "TH",
  TRY: "TR",
  TWD: "TW",
  TZS: "TZ",
  UGX: "UG",
  USD: "US",
  VND: "VN",
  ZAR: "ZA",
  ZMW: "ZM"
};
var CURRENCY_TO_CANDIDATE_COUNTRIES = {
  EUR: ["DE", "FR", "IT", "ES", "NL", "BE", "AT", "PT", "IE", "FI", "GR"],
  USD: ["US", "EC", "PA", "SV"],
  XOF: ["SN", "CI", "BF", "ML", "NE", "TG", "BJ", "GW"],
  XAF: ["CM", "GA", "CG", "TD", "CF", "GQ"]
};
function normalizeCountryCode(value) {
  const normalized = value?.trim().toUpperCase();
  if (!normalized) {
    return void 0;
  }
  if (normalized.length === 2) {
    return normalized;
  }
  return ISO3_TO_COUNTRY_CODE[normalized];
}
function parsePhoneCandidate(value, country) {
  try {
    return country ? parsePhoneNumberFromString(value, country) : parsePhoneNumberFromString(value);
  } catch {
    return void 0;
  }
}
function resolvePhoneCountryCode(options = {}) {
  const explicitCountry = normalizeCountryCode(options.country);
  if (explicitCountry) {
    return explicitCountry;
  }
  const currency = options.currency?.trim().toUpperCase();
  if (!currency) {
    return void 0;
  }
  return CURRENCY_TO_COUNTRY_CODE[currency];
}
function normalizePhoneToE164(raw, options = {}) {
  const trimmed = raw.trim();
  if (!trimmed) {
    return null;
  }
  const country = resolvePhoneCountryCode(options);
  const digits = trimmed.replace(/\D/g, "");
  const internationalDigits = digits.startsWith("00") ? digits.slice(2) : digits;
  const attempts = [];
  const seen = /* @__PURE__ */ new Set();
  function addAttempt(value, nextCountry) {
    const key = `${nextCountry || ""}:${value}`;
    if (seen.has(key)) {
      return;
    }
    seen.add(key);
    attempts.push({ value, country: nextCountry });
  }
  if (country) {
    addAttempt(trimmed, country);
  }
  addAttempt(trimmed);
  if (country && digits) {
    addAttempt(digits, country);
  }
  if (internationalDigits) {
    addAttempt(`+${internationalDigits}`);
  }
  const currency = options.currency?.trim().toUpperCase();
  if (currency) {
    const candidates = CURRENCY_TO_CANDIDATE_COUNTRIES[currency];
    if (candidates) {
      for (const candidate of candidates) {
        addAttempt(trimmed, candidate);
        if (digits) addAttempt(digits, candidate);
      }
    }
  }
  for (const attempt of attempts) {
    const parsed = parsePhoneCandidate(attempt.value, attempt.country);
    if (parsed?.isValid()) {
      return parsed.format("E.164");
    }
  }
  return null;
}
function isValidPhoneForContext(raw, options = {}) {
  return normalizePhoneToE164(raw, options) !== null;
}

export { isValidPhoneForContext, normalizePhoneToE164, resolvePhoneCountryCode };
