const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_index = require("../../../select/index.js");
const require_useSetRequest = require("../../../../hooks/useSetRequest.js");
const require_useTranslationOriginal = require("../../../../hooks/useTranslationOriginal.js");
const require_utils = require("../../dataSourceForm/utils.js");
const require_serve = require("./serve.js");
const require_ReadPretty = require("./ReadPretty.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let antd = require("antd");
//#region src/components/dataSourceComponents/fields/Input.Phone/WithMode.tsx
const _excluded = [
	"value",
	"onChange",
	"disabled",
	"returnFormat"
];
const { Option } = require_index.default;
/**
* 电话号码输入组件
* 
* 校验规则：
* 使用 libphonenumber-js 进行校验
* 1. 清除所有空格和括号等特殊字符
* 2. 验证是否符合 E.164 格式
* 3. 验证号码是否在对应国家有效
* 
* 格式示例：
* - 国际格式：+86 10 1234 5678
* - 国内格式：010 1234 5678
* 
* 使用方式：
* <Phone
*   name="phone"
*   label="电话号码"
*   typeobj={{
*     type: 'phone',
*     enabled: true
*   }}
* />
*/
const PhoneNumberInput = (_ref) => {
	let { value: propsValue, onChange, disabled, returnFormat = "string" } = _ref, props = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded);
	require_useSetRequest.default();
	const { data: countries = [] } = require_serve.useCountries();
	const translationOriginal = require_useTranslationOriginal.default();
	const value = (0, react.useMemo)(() => {
		if ((0, _pisell_utils.isNumber)(propsValue)) return `${propsValue}`;
		return propsValue;
	}, [propsValue]);
	const parseExternalValue = (val) => {
		if (!val) return {
			countryCode: "AU",
			phoneNumber: ""
		};
		let srtValue = "";
		if ((0, _pisell_utils.isString)(val)) srtValue = val;
		else if ("phone" in val) srtValue = `${val.countryCallingCode || ""}${val.phone || ""}`;
		const matchedPrefix = countries.find((country) => srtValue.startsWith(country.prefix));
		if (matchedPrefix) return {
			countryCode: matchedPrefix.code,
			phoneNumber: srtValue.slice(matchedPrefix.prefix.length)
		};
		return {
			countryCode: "AU",
			phoneNumber: srtValue.replace(/\D/g, "")
		};
	};
	const [countryCode, setCountryCode] = (0, react.useState)(() => parseExternalValue(value).countryCode);
	const [phoneNumber, setPhoneNumber] = (0, react.useState)(() => parseExternalValue(value).phoneNumber);
	(0, react.useEffect)(() => {
		const { countryCode: newCountryCode, phoneNumber: newPhoneNumber } = parseExternalValue(value);
		setCountryCode(newCountryCode);
		setPhoneNumber(newPhoneNumber);
	}, [value, countries]);
	const handleChange = (prefix, phoneNumber) => {
		if (returnFormat === "string") {
			const newValue = phoneNumber ? `${prefix}${phoneNumber}` : "";
			onChange === null || onChange === void 0 || onChange(newValue);
		} else onChange === null || onChange === void 0 || onChange({
			phone: phoneNumber,
			countryCallingCode: prefix
		});
	};
	const handlePhoneNumberChange = (e) => {
		const cleanNumber = e.target.value.replace(/\D/g, "");
		setPhoneNumber(cleanNumber);
		const selectedCountry = countries.find((c) => c.code === countryCode);
		handleChange((selectedCountry === null || selectedCountry === void 0 ? void 0 : selectedCountry.prefix) || "", cleanNumber);
		if (!cleanNumber) setTimeout(() => {
			const formInstance = props.form;
			const fieldName = props.fieldKey;
			if (formInstance && fieldName) formInstance.validateFields([fieldName]);
		});
	};
	const handleCountryChange = (value) => {
		setCountryCode(value);
		const selectedCountry = countries.find((c) => c.code === value);
		handleChange((selectedCountry === null || selectedCountry === void 0 ? void 0 : selectedCountry.prefix) || "", phoneNumber);
		setTimeout(() => {
			const formInstance = props.form;
			const fieldName = props.fieldKey;
			if (formInstance && fieldName) formInstance.validateFields([fieldName]);
		});
	};
	const concatenatedNumber = (0, react.useMemo)(() => {
		const selectedCountry = countries.find((c) => c.code === countryCode);
		if (phoneNumber.length === 0) return "";
		return `${(selectedCountry === null || selectedCountry === void 0 ? void 0 : selectedCountry.prefix) || ""}${phoneNumber}`;
	}, [
		countryCode,
		phoneNumber,
		countries
	]);
	const objValue = (0, react.useMemo)(() => {
		const selectedCountry = countries.find((c) => c.code === countryCode);
		const prefix = (selectedCountry === null || selectedCountry === void 0 ? void 0 : selectedCountry.prefix) || "";
		return {
			phone: phoneNumber || "",
			countryCallingCode: prefix
		};
	}, [
		countryCode,
		phoneNumber,
		countries
	]);
	const handleBlur = () => {
		const finalValue = concatenatedNumber;
		if (returnFormat === "string") onChange === null || onChange === void 0 || onChange(finalValue);
		else onChange === null || onChange === void 0 || onChange(objValue);
	};
	return /* @__PURE__ */ react.default.createElement(antd.Space.Compact, { block: true }, /* @__PURE__ */ react.default.createElement(require_index.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__ */ react.default.createElement(antd.Input, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, props), {}, {
		ref: props === null || props === void 0 ? void 0 : props.inputRef,
		disabled,
		type: "tel",
		value: phoneNumber,
		onChange: handlePhoneNumberChange,
		onBlur: handleBlur,
		style: { flex: 1 }
	})));
};
const PhoneNumberInputWithMode = require_utils.withMode(PhoneNumberInput, require_ReadPretty.default);
//#endregion
exports.default = PhoneNumberInputWithMode;
