"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.validatePhoneNumber = exports.getPhoneUtil = exports.formatPhone = void 0;
let phoneUtil = null;
const getPhoneUtil = async () => {
  if (!phoneUtil) {
    const result = await Promise.resolve().then(() => require("google-libphonenumber"));
    const PhoneNumberUtil = (result.default || result)?.PhoneNumberUtil;
    if (!PhoneNumberUtil) {
      throw new Error("PhoneNumberUtil not found");
    }
    phoneUtil = PhoneNumberUtil.getInstance();
  }
  return phoneUtil;
};
exports.getPhoneUtil = getPhoneUtil;
const validatePhoneNumber = async phoneNumber => {
  if (!phoneNumber) return true;
  try {
    let util = null;
    try {
      util = await getPhoneUtil();
    } catch (err) {
      const pattern = /^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/im;
      return pattern.test(phoneNumber);
    }
    const parsed = util.parseAndKeepRawInput(phoneNumber);
    return util.isValidNumber(parsed);
  } catch (err) {
    console.error("Phone validation error:", err);
    return false;
  }
};
exports.validatePhoneNumber = validatePhoneNumber;
const formatPhone = (phoneNumber, defaultCountry = "US") => {
  if (!phoneNumber || phoneNumber.trim() === "") {
    return "";
  }
  const cleanedNumber = phoneNumber.replace(/[^\d+]/g, "");
  if (cleanedNumber.startsWith("+")) {
    return cleanedNumber;
  }
  const COUNTRY_CODES = {
    US: "1",
    // United States
    CA: "1",
    // Canada
    CN: "86",
    // China
    HK: "852",
    // Hong Kong
    IN: "91",
    // India
    UK: "44",
    // United Kingdom
    GB: "44",
    // United Kingdom
    JP: "81",
    // Japan
    KR: "82",
    // South Korea
    AU: "61",
    // Australia
    DE: "49",
    // Germany
    FR: "33",
    // France
    IT: "39",
    // Italy
    ES: "34",
    // Spain
    BR: "55",
    // Brazil
    RU: "7",
    // Russia
    MX: "52",
    // Mexico
    SG: "65",
    // Singapore
    AE: "971"
    // UAE
  };
  const COUNTRY_PATTERNS = [[/^1[3-9]\d{9}$/, "86"],
  // China mobile: 11 digits, starts with 1
  [/^\d{10}$/, "1"],
  // US/Canada: 10 digits
  [/^[6-9]\d{9}$/, "91"],
  // India: 10 digits, starts with 6-9
  [/^0[1-9]\d{8,9}$/, "81"],
  // Japan: 10-11 digits, starts with 0
  [/^07\d{9}$/, "44"],
  // UK mobile: 11 digits, starts with 07
  [/^04\d{8}$/, "61"],
  // Australia mobile: 10 digits, starts with 04
  [/^01[0-9]\d{7,8}$/, "82"],
  // Korea mobile: 10-11 digits, starts with 01
  [/^[0-9]\d{8}$/, "86"]
  // China landline: 9 digits
  ];
  let numberToFormat = cleanedNumber;
  if (numberToFormat.startsWith("0")) {
    numberToFormat = numberToFormat.substring(1);
  }
  for (const [pattern, countryCode2] of COUNTRY_PATTERNS) {
    if (pattern.test(cleanedNumber)) {
      if (cleanedNumber.startsWith("0") && !["1", "86"].includes(countryCode2)) {
        return `+${countryCode2}${cleanedNumber.substring(1)}`;
      }
      return `+${countryCode2}${cleanedNumber}`;
    }
  }
  const countryCode = COUNTRY_CODES[defaultCountry.toUpperCase()] || defaultCountry;
  return `+${countryCode}${numberToFormat}`;
};
exports.formatPhone = formatPhone;