const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
//#region src/pro/Login2.0/components/phoneValidation.ts
/**
* 手机号校验函数，用于 Ant Design Form 的 rules
* @param value 手机号值，可以是字符串或对象格式
* @param getText 国际化文本获取函数
* @returns Promise<void> 校验通过时 resolve，失败时 reject 并返回错误信息
*/
const validatePhoneNumber = function() {
	var _ref = require_asyncToGenerator._asyncToGenerator(function* (value, getText) {
		if (!value) throw new Error(getText("pisell-phone-validation-required"));
		let phone;
		let countryCode;
		if (typeof value === "string") {
			if (!value.startsWith("+")) throw new Error(getText("pisell-phone-validation-invalid"));
			const match = value.match(/^\+(\d{1,4})(\d+)$/);
			if (!match) throw new Error(getText("pisell-phone-validation-invalid"));
			countryCode = match[1];
			phone = match[2];
		} else {
			phone = value.phone;
			countryCode = value.country_calling_code;
		}
		if (!phone || !countryCode) throw new Error(getText("pisell-phone-validation-required"));
		switch (countryCode) {
			case "86":
				if (!/^1[3-9]\d{9}$/.test(phone)) throw new Error(getText("pisell-phone-validation-china"));
				break;
			case "852":
				if (!/^[5-9]\d{7}$/.test(phone)) throw new Error(getText("pisell-phone-validation-hongkong"));
				break;
			case "853":
				if (!/^6\d{7}$/.test(phone)) throw new Error(getText("pisell-phone-validation-macau"));
				break;
			case "886":
				if (!/^9\d{8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-taiwan"));
				break;
			case "1":
				if (!/^[2-9]\d{2}[2-9]\d{6}$/.test(phone)) throw new Error(getText("pisell-phone-validation-us-canada"));
				break;
			case "44":
				if (!/^7\d{9}$/.test(phone)) throw new Error(getText("pisell-phone-validation-uk"));
				break;
			case "61":
				if (!/^0?[2-578]\d{8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-australia"));
				break;
			case "81":
				if (!/^[789]0\d{8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-japan"));
				break;
			case "82":
				if (!/^1[0-9]\d{7,8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-korea"));
				break;
			case "65":
				if (!/^[89]\d{7}$/.test(phone)) throw new Error(getText("pisell-phone-validation-singapore"));
				break;
			case "60":
				if (!/^1[0-9]\d{7,8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-malaysia"));
				break;
			case "66":
				if (!/^[689]\d{8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-thailand"));
				break;
			case "84":
				if (!/^[3579]\d{8}$/.test(phone)) throw new Error(getText("pisell-phone-validation-vietnam"));
				break;
			case "63":
				if (!/^9\d{9}$/.test(phone)) throw new Error(getText("pisell-phone-validation-philippines"));
				break;
			case "62":
				if (!/^8\d{8,11}$/.test(phone)) throw new Error(getText("pisell-phone-validation-indonesia"));
				break;
			case "91":
				if (!/^[6-9]\d{9}$/.test(phone)) throw new Error(getText("pisell-phone-validation-india"));
				break;
			default:
				if (!/^\d{6,15}$/.test(phone)) throw new Error(getText("pisell-phone-validation-invalid"));
				break;
		}
	});
	return function validatePhoneNumber(_x, _x2) {
		return _ref.apply(this, arguments);
	};
}();
/**
* 创建手机号校验规则，用于 Form.Item 的 rules 属性
* @param getText 国际化文本获取函数
* @param required 是否必填
* @returns Form 校验规则数组
*/
const createPhoneValidationRules = (getText, required = true) => {
	const rules = [];
	rules.push({ validator: (_, value) => {
		if (!value && !required) return Promise.resolve();
		return validatePhoneNumber(value, getText);
	} });
	return rules;
};
//#endregion
exports.createPhoneValidationRules = createPhoneValidationRules;
