import { jsxs as D, jsx as y } from "react/jsx-runtime";
import { useMemo as g, useState as F, useCallback as k } from "react";
import { Box as $, Typography as A, TextField as L } from "@mui/material";
import { parsePhoneNumber as b } from "awesome-phonenumber";
import { defaultCountries as d, parseCountry as f, usePhoneInput as R } from "react-international-phone";
import I from "./country-select.js";
import { mergeSx as q } from "../Util/style.js";
function x(e, t, n = "") {
  if (!e) return !1;
  const r = e.replace(/[\s\-()]+/g, ""), u = n || w(t);
  if (m(u) === m(r))
    return !0;
  const a = b(e, { regionCode: t }), l = a.number?.input.replace(/[\s\-()]+/g, "");
  return a.valid && (a.type === "mobile" || a.type === "fixed-line-or-mobile") && l === a.number?.e164;
}
function P(e, t) {
  return e ? e.replace(new RegExp(`^\\+${t}`), "") : "";
}
function V(e, t) {
  return e ? e.startsWith("+") ? e : `+${t}${e}` : `+${t}`;
}
function m(e) {
  return e.replace(/^\+/, "");
}
function v(e, t) {
  const n = d.find((r) => f(r).iso2 === e);
  if (n)
    return f(n)[t];
}
function w(e) {
  return v(e, "dialCode") || "";
}
function U(e) {
  return v(e, "name") || "";
}
function X(e) {
  try {
    if (!e) return;
    if (e.startsWith("+")) {
      const t = b(e);
      if (t.valid)
        return t.regionCode.toLowerCase();
      for (const n of d) {
        const r = f(n);
        if (e.startsWith(`+${r.dialCode}`) && x(e, r.dialCode, r.iso2))
          return r.iso2;
      }
    } else
      for (const t of d) {
        const n = f(t), r = `+${n.dialCode}${e}`;
        if (x(r, n.dialCode, n.iso2))
          return n.iso2;
      }
    return;
  } catch {
    return;
  }
}
function Y({
  value: e = { country: "us", phone: "" },
  onChange: t = void 0,
  placeholder: n = "Enter phone number",
  countryDisplayOptions: r = {},
  preview: u = !1,
  allowDial: a = !0,
  ...l
}) {
  const p = g(() => {
    const o = /* @__PURE__ */ new Map();
    return d.forEach((i) => {
      const s = f(i);
      o.set(s.iso2, s);
    }), o;
  }, []), [B, M] = F(e), E = k(
    (o) => {
      if (!u && t) {
        const i = {
          ...e,
          ...o
        }, s = p.get(i.country);
        s && (i.phone = V(i.phone, m(s.dialCode))), t(i);
      }
    },
    [t, u, e, p]
  ), h = g(() => {
    const o = p.get(e.country);
    return o ? m(o.dialCode) : "";
  }, [e.country, p]), { phone: c, country: C, handlePhoneValueChange: S, setCountry: T } = R({
    defaultCountry: e.country,
    value: P(e.phone, h),
    countries: d,
    disableDialCodeAndPrefix: !0,
    onChange: (o) => {
      const i = {
        ...o,
        phone: B.country !== o.country && !o.phone ? P(e.phone, h) : o.phone
      };
      M(i), E(i);
    }
  }), W = g(
    () => c || P(e.phone, h),
    [c, e.phone, h]
  ), j = (o) => {
    u || T(o);
  };
  if (u) {
    const o = V(c, w(C)), i = c && x(o, C), s = a && i;
    return /* @__PURE__ */ D(
      $,
      {
        component: s ? "a" : "div",
        href: s ? `tel:${c}` : void 0,
        ...s ? {
          "aria-label": `Call ${c}`,
          title: `Call ${c}`,
          rel: "nofollow",
          itemProp: "telephone"
        } : {},
        sx: {
          display: "flex",
          alignItems: "center",
          textDecoration: "none",
          color: "inherit",
          cursor: s ? "pointer" : "default",
          "&:hover": {
            opacity: s ? 0.8 : 1
          }
        },
        children: [
          /* @__PURE__ */ y(I, { value: C, preview: u }),
          /* @__PURE__ */ y(A, { sx: { ml: 0.5 }, children: W })
        ]
      }
    );
  }
  return /* @__PURE__ */ D(
    $,
    {
      sx: {
        display: "flex",
        alignItems: "flex-start",
        gap: 0.5
      },
      children: [
        /* @__PURE__ */ y(
          I,
          {
            value: C,
            onChange: j,
            preview: !1,
            selectCountryProps: r
          }
        ),
        /* @__PURE__ */ y(
          L,
          {
            ...l,
            value: W,
            onChange: S,
            placeholder: n,
            className: "phone-input",
            sx: q(
              {
                "& .MuiInputBase-input": {
                  padding: 1
                }
              },
              l.sx
              // 这里传入的可能是一个数组或一个对象
            ),
            slotProps: {
              input: l.InputProps ?? {}
            }
          }
        )
      ]
    }
  );
}
export {
  Y as default,
  X as detectCountryFromPhone,
  U as getCountryNameByCountry,
  w as getDialCodeByCountry,
  x as validatePhoneNumber
};
