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/components/dataSourceComponents/fields/Input.Phone/WithMode.tsx
var WithMode_exports = {};
__export(WithMode_exports, {
  default: () => WithMode_default
});
module.exports = __toCommonJS(WithMode_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_utils = require("@pisell/utils");
var import_select = __toESM(require("../../../select"));
var import_serve = require("./serve");
var import_ReadPretty = __toESM(require("./ReadPretty"));
var import_utils2 = require("../../dataSourceForm/utils");
var import_useTranslationOriginal = __toESM(require("../../../../hooks/useTranslationOriginal"));
var { Option } = import_select.default;
var PhoneNumberInput = ({ value: propsValue, onChange, disabled, ...props }) => {
  const { data: countries = [] } = (0, import_serve.useCountries)();
  const translationOriginal = (0, import_useTranslationOriginal.default)();
  const value = (0, import_react.useMemo)(() => {
    if ((0, import_utils.isNumber)(propsValue)) {
      return `${propsValue}`;
    }
    return propsValue;
  }, [propsValue]);
  const parseExternalValue = (val) => {
    if (!val)
      return { countryCode: "AU", phoneNumber: "" };
    const matchedPrefix = countries.find(
      (country) => val.startsWith(country.prefix)
    );
    if (matchedPrefix) {
      return {
        countryCode: matchedPrefix.code,
        phoneNumber: val.slice(matchedPrefix.prefix.length)
      };
    }
    return {
      countryCode: "AU",
      phoneNumber: val.replace(/\D/g, "")
    };
  };
  const [countryCode, setCountryCode] = (0, import_react.useState)(
    () => parseExternalValue(value).countryCode
  );
  const [phoneNumber, setPhoneNumber] = (0, import_react.useState)(
    () => parseExternalValue(value).phoneNumber
  );
  (0, import_react.useEffect)(() => {
    const { countryCode: newCountryCode, phoneNumber: newPhoneNumber } = parseExternalValue(value);
    setCountryCode(newCountryCode);
    setPhoneNumber(newPhoneNumber);
  }, [value, countries]);
  const handlePhoneNumberChange = (e) => {
    const cleanNumber = e.target.value.replace(/\D/g, "");
    setPhoneNumber(cleanNumber);
    const selectedCountry = countries.find(
      (c) => c.code === countryCode
    );
    const prefix = (selectedCountry == null ? void 0 : selectedCountry.prefix) || "";
    const newValue = cleanNumber ? `${prefix}${cleanNumber}` : "";
    onChange == null ? void 0 : onChange(newValue);
    if (!cleanNumber) {
      setTimeout(() => {
        const formInstance = props.form;
        const fieldName = props.fieldKey;
        if (formInstance && fieldName) {
          formInstance.validateFields([fieldName]);
        }
      });
    }
  };
  const handleCountryChange = (value2) => {
    setCountryCode(value2);
    const selectedCountry = countries.find(
      (c) => c.code === value2
    );
    const prefix = (selectedCountry == null ? void 0 : selectedCountry.prefix) || "";
    const newValue = `${prefix}${phoneNumber}`;
    onChange == null ? void 0 : onChange(newValue);
    setTimeout(() => {
      const formInstance = props.form;
      const fieldName = props.fieldKey;
      if (formInstance && fieldName) {
        formInstance.validateFields([fieldName]);
      }
    });
  };
  const concatenatedNumber = (0, import_react.useMemo)(() => {
    const selectedCountry = countries.find(
      (c) => c.code === countryCode
    );
    if (phoneNumber.length === 0) {
      return "";
    }
    const prefix = (selectedCountry == null ? void 0 : selectedCountry.prefix) || "";
    return `${prefix}${phoneNumber}`;
  }, [countryCode, phoneNumber, countries]);
  const handleBlur = () => {
    const finalValue = concatenatedNumber;
    onChange == null ? void 0 : onChange(finalValue);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Space.Compact, { block: true }, /* @__PURE__ */ import_react.default.createElement(
    import_select.default,
    {
      disabled,
      value: countryCode,
      onChange: handleCountryChange,
      style: { width: "90px" },
      popupMatchSelectWidth: false,
      optionLabelProp: "inputValue",
      optionFilterProp: "label",
      showSearch: true,
      options: countries.map((country) => ({
        value: country.code,
        label: `${translationOriginal(country.name)} (${country.prefix})`,
        inputValue: country.prefix
      }))
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_antd.Input,
    {
      ...props,
      disabled,
      type: "tel",
      value: phoneNumber,
      onChange: handlePhoneNumberChange,
      onBlur: handleBlur,
      style: { flex: 1 }
    }
  ));
};
var PhoneNumberInputWithMode = (0, import_utils2.withMode)(PhoneNumberInput, import_ReadPretty.default);
var WithMode_default = PhoneNumberInputWithMode;
