var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/Login2.0/components/PhoneInput.tsx
var PhoneInput_exports = {};
__export(PhoneInput_exports, {
  default: () => PhoneInput_default
});
module.exports = __toCommonJS(PhoneInput_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_PhoneInput = require("./PhoneInput.less");
var DEFAULT_COUNTRIES = [
  { name: "United States", code: "US", calling_code: "1", currency_code: "USD" },
  { name: "China", code: "CN", calling_code: "86", currency_code: "CNY" },
  { name: "United Kingdom", code: "GB", calling_code: "44", currency_code: "GBP" },
  { name: "Australia", code: "AU", calling_code: "61", currency_code: "AUD" },
  { name: "Canada", code: "CA", calling_code: "1", currency_code: "CAD" },
  { name: "Japan", code: "JP", calling_code: "81", currency_code: "JPY" },
  { name: "South Korea", code: "KR", calling_code: "82", currency_code: "KRW" },
  { name: "Singapore", code: "SG", calling_code: "65", currency_code: "SGD" },
  { name: "Hong Kong", code: "HK", calling_code: "852", currency_code: "HKD" },
  { name: "Taiwan", code: "TW", calling_code: "886", currency_code: "TWD" },
  { name: "Malaysia", code: "MY", calling_code: "60", currency_code: "MYR" },
  { name: "Thailand", code: "TH", calling_code: "66", currency_code: "THB" },
  { name: "Vietnam", code: "VN", calling_code: "84", currency_code: "VND" },
  { name: "Philippines", code: "PH", calling_code: "63", currency_code: "PHP" },
  { name: "Indonesia", code: "ID", calling_code: "62", currency_code: "IDR" },
  { name: "India", code: "IN", calling_code: "91", currency_code: "INR" },
  { name: "Germany", code: "DE", calling_code: "49", currency_code: "EUR" },
  { name: "France", code: "FR", calling_code: "33", currency_code: "EUR" },
  { name: "Italy", code: "IT", calling_code: "39", currency_code: "EUR" },
  { name: "Spain", code: "ES", calling_code: "34", currency_code: "EUR" }
];
function detectUserCountry() {
  try {
    if (typeof Intl !== "undefined" && Intl.DateTimeFormat) {
      const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
      const timeZoneMap = {
        "America/New_York": "US",
        "America/Los_Angeles": "US",
        "America/Chicago": "US",
        "America/Denver": "US",
        "America/Toronto": "CA",
        "America/Vancouver": "CA",
        "Europe/London": "GB",
        "Europe/Paris": "FR",
        "Europe/Berlin": "DE",
        "Europe/Rome": "IT",
        "Europe/Madrid": "ES",
        "Asia/Shanghai": "CN",
        "Asia/Hong_Kong": "HK",
        "Asia/Taipei": "TW",
        "Asia/Tokyo": "JP",
        "Asia/Seoul": "KR",
        "Asia/Singapore": "SG",
        "Asia/Bangkok": "TH",
        "Asia/Kuala_Lumpur": "MY",
        "Asia/Jakarta": "ID",
        "Asia/Manila": "PH",
        "Asia/Ho_Chi_Minh": "VN",
        "Asia/Kolkata": "IN",
        "Australia/Sydney": "AU",
        "Australia/Melbourne": "AU"
      };
      if (timeZone && timeZoneMap[timeZone]) {
        return timeZoneMap[timeZone];
      }
    }
    if (typeof navigator !== "undefined" && navigator.language) {
      const lang = navigator.language;
      const parts = lang.split("-");
      if (parts.length > 1) {
        const countryCode = parts[1].toUpperCase();
        return countryCode;
      }
      const langMap = {
        "zh": "CN",
        "en": "US",
        "ja": "JP",
        "ko": "KR",
        "de": "DE",
        "fr": "FR",
        "es": "ES",
        "it": "IT",
        "th": "TH",
        "vi": "VN",
        "id": "ID",
        "ms": "MY"
      };
      if (langMap[parts[0]]) {
        return langMap[parts[0]];
      }
    }
  } catch (error) {
    console.warn("Failed to detect user country:", error);
  }
  return null;
}
function PhoneInput({
  value,
  onChange,
  placeholder,
  onKeyDown,
  size = "large",
  disabled = false,
  className = "",
  countries = DEFAULT_COUNTRIES,
  defaultCountry,
  valueFormat = "string"
}) {
  const effectivePlaceholder = placeholder || import_utils.locales.getText("pisell-phone-input-placeholder");
  const normalizedValue = (0, import_react.useMemo)(() => {
    if (!value) return "";
    if (typeof value === "object" && "phone" in value) {
      const { phone, country_calling_code } = value;
      return `+${country_calling_code}${phone}`;
    }
    return value;
  }, [value]);
  const effectiveDefaultCountry = (0, import_react.useMemo)(() => {
    var _a;
    const detectedCountry = detectUserCountry();
    if (detectedCountry && countries.some((c) => c.code === detectedCountry)) {
      console.log("PhoneInput: Auto-detected country:", detectedCountry);
      return detectedCountry;
    }
    if (defaultCountry && countries.some((c) => c.code === defaultCountry)) {
      return defaultCountry;
    }
    return ((_a = countries == null ? void 0 : countries[0]) == null ? void 0 : _a.code) || "AU";
  }, [countries, defaultCountry]);
  const { dialCode: currentDialCode, phoneNumber: currentPhoneNumber } = (0, import_react.useMemo)(() => {
    if (!(countries == null ? void 0 : countries.length)) {
      return {
        dialCode: "",
        phoneNumber: ""
      };
    }
    if (!normalizedValue || !normalizedValue.startsWith("+")) {
      const defaultCountryOption2 = countries.find((c) => c.code === effectiveDefaultCountry) || countries[0];
      return {
        dialCode: `+${defaultCountryOption2.calling_code}`,
        phoneNumber: ""
      };
    }
    for (const country of countries) {
      const countryDialCode = `+${country.calling_code}`;
      if (normalizedValue.startsWith(countryDialCode)) {
        return {
          dialCode: countryDialCode,
          phoneNumber: normalizedValue.slice(countryDialCode.length)
        };
      }
    }
    const defaultCountryOption = countries.find((c) => c.code === effectiveDefaultCountry) || countries[0];
    return {
      dialCode: `+${defaultCountryOption.calling_code}`,
      phoneNumber: normalizedValue.startsWith("+") ? normalizedValue.slice(1) : normalizedValue
    };
  }, [normalizedValue, countries, effectiveDefaultCountry]);
  const currentCountry = (0, import_react.useMemo)(() => {
    return countries.find((c) => `+${c.calling_code}` === currentDialCode) || countries[0];
  }, [currentDialCode, countries]);
  const formatOutputValue = (dialCode, phoneNumber) => {
    const fullString = `${dialCode}${phoneNumber}`;
    if (valueFormat === "object") {
      return {
        phone: String(phoneNumber || ""),
        country_calling_code: String(dialCode.replace("+", "") || "")
        // 去掉 + 号并确保是字符串
      };
    }
    return fullString;
  };
  const handleDialCodeChange = (e) => {
    const newDialCode = e.target.value;
    const newValue = formatOutputValue(newDialCode, currentPhoneNumber);
    onChange == null ? void 0 : onChange(newValue);
  };
  const handlePhoneNumberChange = (e) => {
    let inputValue = e.target.value;
    const dialCodeWithoutPlus = currentDialCode.replace("+", "");
    if (inputValue.startsWith("+")) {
      if (inputValue.startsWith(currentDialCode)) {
        inputValue = inputValue.slice(currentDialCode.length);
      }
    } else if (inputValue.startsWith(dialCodeWithoutPlus)) {
      inputValue = inputValue.slice(dialCodeWithoutPlus.length);
    }
    const newPhoneNumber = inputValue.replace(/[^\d]/g, "");
    const newValue = formatOutputValue(currentDialCode, newPhoneNumber);
    onChange == null ? void 0 : onChange(newValue);
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: `phone-input-wrapper ${className}` }, /* @__PURE__ */ import_react.default.createElement("div", { className: "phone-input-container" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "phone-input-prefix" }, /* @__PURE__ */ import_react.default.createElement("span", { className: "phone-input-country-display" }, currentCountry == null ? void 0 : currentCountry.code, "(", currentDialCode, ")"), /* @__PURE__ */ import_react.default.createElement(
    "select",
    {
      className: "phone-input-select",
      value: currentDialCode,
      onChange: handleDialCodeChange,
      disabled
    },
    countries.map((country) => /* @__PURE__ */ import_react.default.createElement("option", { key: country.code, value: `+${country.calling_code}` }, country.code, " +", country.calling_code))
  ), /* @__PURE__ */ import_react.default.createElement(
    "svg",
    {
      className: "phone-input-arrow",
      width: "12",
      height: "12",
      viewBox: "0 0 12 12",
      fill: "none",
      xmlns: "http://www.w3.org/2000/svg"
    },
    /* @__PURE__ */ import_react.default.createElement(
      "path",
      {
        d: "M3 4.5L6 7.5L9 4.5",
        stroke: "currentColor",
        strokeWidth: "1.5",
        strokeLinecap: "round",
        strokeLinejoin: "round"
      }
    )
  )), /* @__PURE__ */ import_react.default.createElement("div", { className: "phone-input-divider" }), /* @__PURE__ */ import_react.default.createElement(
    "input",
    {
      type: "tel",
      className: `phone-input-field phone-input-field-${size}`,
      value: currentPhoneNumber,
      onChange: handlePhoneNumberChange,
      onKeyDown,
      placeholder: effectivePlaceholder,
      disabled,
      autoComplete: "tel"
    }
  )));
}
var PhoneInput_default = PhoneInput;
