const require_runtime = require("../../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_model = require("../../../model.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/pay/toC/PaymentMethods/MWCreditCard/Add/index.tsx
/**
* @title: 格式化名称
* @description:
* @param {string} card_name
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:12
*/
const formatName = (card_name) => {
	let _name = card_name || "";
	_name = _name.trim();
	if (!_name) return {
		text: "",
		error: _pisell_utils.locales.getText("tocPay.text.enterName")
	};
	_name = _name.replace("-", "");
	_name = _name.replace(/\s+/g, " ");
	let _arr = _name.split(" ");
	if (_arr.length == 0) return {
		text: "",
		error: _pisell_utils.locales.getText("tocPay.text.enterValidName")
	};
	if (_arr.length == 1) {
		_name = _name;
		return {
			text: _name,
			error: ""
		};
	} else {
		_name = _arr[0] + " " + _arr[_arr.length - 1];
		return {
			text: _name,
			error: ""
		};
	}
};
/**
* @title: 添加信用卡
* @description:
* @param {AddMWCreditCardProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:13
*/
const AddMWCreditCard = (props, ref) => {
	const { onCreate, shopName = "", btnLoading = false } = props;
	const [open, setOpen] = (0, react.useState)(false);
	const [form] = antd.Form.useForm();
	const { state } = (0, react.useContext)(require_model.Context);
	/**
	* @title: 初始化
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:13
	*/
	const init = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			onReset();
			setOpen(true);
		});
		return function init() {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 取消
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:13
	*/
	const onCancel = () => {
		setOpen(false);
		onReset();
	};
	/**
	* @title: 检测表单是否正确
	* @description:
	* @param {any} values
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:13
	*/
	const _checkValue = (values) => {
		if (!values.card_number) {
			console.log(_pisell_utils.locales.getText("tocPay.text.enterCardNumber"));
			return;
		}
		if (values.card_number.replace(/\s/g, "").length < 16) {
			(0, _pisell_materials.PisellToast)({
				content: _pisell_utils.locales.getText("tocPay.text.enterValidCardNumber"),
				duration: 2
			});
			console.log(_pisell_utils.locales.getText("tocPay.text.enterValidCardNumber"));
			return;
		}
		let { error, text } = formatName(values.card_name);
		if (error) {
			console.log(error);
			return;
		}
		if (!/^([A-Za-z]+|[A-Za-z,.'&-]+) ([A-Za-z]+|[A-Za-z,.'&-]+)$/.test(text)) {
			(0, _pisell_materials.PisellToast)({
				content: _pisell_utils.locales.getText("tocPay.text.enterEnglishName"),
				duration: 2
			});
			console.log(_pisell_utils.locales.getText("tocPay.text.enterEnglishName"));
			return;
		}
		if (!values.date) {
			(0, _pisell_materials.PisellToast)({
				content: _pisell_utils.locales.getText("tocPay.text.enterExpirationDate"),
				duration: 2
			});
			console.log(_pisell_utils.locales.getText("tocPay.text.enterExpirationDate"));
			return;
		}
		if (!values.card_ccv) {
			(0, _pisell_materials.PisellToast)({
				content: _pisell_utils.locales.getText("tocPay.text.enterCVV"),
				duration: 2
			});
			console.log(_pisell_utils.locales.getText("tocPay.text.enterCVV"));
			return;
		}
		if ((values.card_ccv + "").length < 3) {
			(0, _pisell_materials.PisellToast)({
				content: _pisell_utils.locales.getText("tocPay.text.enterValidCVV"),
				duration: 2
			});
			console.log(_pisell_utils.locales.getText("tocPay.text.enterValidCVV"));
			return;
		}
		return true;
	};
	/**
	* @title: 重置表单
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:14
	*/
	const onReset = () => {
		form.resetFields();
	};
	/**
	* @title: 提交
	* @description:
	* @param {any} values
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:14
	*/
	const onFinish = (values) => {
		if (!_checkValue(values)) return;
		onCreate === null || onCreate === void 0 || onCreate(values);
	};
	const onSubmit = () => {
		form.submit();
	};
	/**
	* @title: 关闭弹窗
	* @description: 供外部调用，直接关闭添加卡片弹窗
	*/
	const close = () => {
		setOpen(false);
	};
	(0, react.useImperativeHandle)(ref, () => ({
		init,
		onReset,
		onSubmit,
		close
	}));
	const okText = (0, react.useMemo)(() => {
		var _state$payGroup;
		let current = (_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.otherPayment;
		if (current === null || current === void 0 ? void 0 : current.surcharge) {
			var _state$payGroup2;
			return /* @__PURE__ */ react.default.createElement("div", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.wait_amount) || 0 }), " +", _pisell_utils.locales.getText("tocPay.text.surcharge"));
		} else {
			var _state$payGroup3;
			return /* @__PURE__ */ react.default.createElement("div", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.wait_amount) || 0 }));
		}
	}, [state.payGroup]);
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		title: /* @__PURE__ */ react.default.createElement("div", { className: "secure-card-payment-title" }, _pisell_utils.locales.getText("tocPay.text.secureCardPayment"), /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
			type: "pisell2-shield-tick",
			className: "secure-card-payment-title-icon"
		})),
		open,
		onCancel,
		cancelVisible: false,
		onOk: onSubmit,
		okText,
		bodyStyle: {
			padding: 24,
			display: "flex",
			flexDirection: "column"
		},
		okButtonProps: {
			loading: btnLoading,
			style: {
				display: "flex",
				justifyContent: "center",
				alignItems: "center"
			}
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "add-mw-credit-card-wrap" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellAlert, {
		className: "secure-card-tip-alert",
		icon: /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, { type: "pisell2-shield-tick" }),
		showIcon: true,
		message: _pisell_utils.locales.getText("tocPay.text.secureBankInfo")
	}), /* @__PURE__ */ react.default.createElement(antd.Form, {
		layout: "vertical",
		form,
		onFinish,
		requiredMark: false
	}, /* @__PURE__ */ react.default.createElement(antd.Row, {
		style: { width: "100%" },
		gutter: [10, 0]
	}, /* @__PURE__ */ react.default.createElement(antd.Col, { span: 24 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: _pisell_utils.locales.getText("tocPay.text.cardNumber"),
		name: "card_number",
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("tocPay.text.incorrectCardNumber")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellInput.BankCard, null))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: _pisell_utils.locales.getText("tocPay.text.expiration"),
		name: "date",
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("tocPay.text.incorrectExpiryDate")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellDatePicker, {
		type: "input",
		format: "MM/YY"
	}))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 12 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: _pisell_utils.locales.getText("tocPay.text.securityCode"),
		name: "card_ccv",
		rules: [{
			required: true,
			message: _pisell_utils.locales.getText("tocPay.text.incorrectSecurityCode")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellInput.SecurityCode, { maxLength: 3 }))), /* @__PURE__ */ react.default.createElement(antd.Col, { span: 24 }, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		label: _pisell_utils.locales.getText("tocPay.text.cardholderName"),
		name: "card_name",
		rules: [{
			required: true,
			pattern: /* @__PURE__ */ new RegExp(/^([A-Za-z]+|[A-Za-z,.'&-]+)( ([A-Za-z]+|[A-Za-z,.'&-]+))+$/, "g"),
			message: _pisell_utils.locales.getText("tocPay.text.enterEnglishName")
		}]
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellInput, { placeholder: _pisell_utils.locales.getText("tocPay.text.incorrectName") })))), /* @__PURE__ */ react.default.createElement("div", { className: "merchant-authorization-text" }, _pisell_utils.locales.getText("tocPay.text.merchantAuthorizationBefore"), /* @__PURE__ */ react.default.createElement("span", { className: "merchant-shop-name" }, shopName), _pisell_utils.locales.getText("tocPay.text.merchantAuthorizationAfter")), /* @__PURE__ */ react.default.createElement(antd.Divider, { className: "secure-card-divider" }), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "is_save",
		valuePropName: "checked",
		initialValue: true,
		className: "save-card-form-item"
	}, /* @__PURE__ */ react.default.createElement(antd.Checkbox, null, /* @__PURE__ */ react.default.createElement("span", { className: "save-card-checkbox-text" }, _pisell_utils.locales.getText("tocPay.text.saveCardForNextTime")))), /* @__PURE__ */ react.default.createElement("div", { className: "save-card-encryption-text" }, _pisell_utils.locales.getText("tocPay.text.saveCardEncryption"))), /* @__PURE__ */ react.default.createElement("div", { className: "three-d-secure-section" }, /* @__PURE__ */ react.default.createElement("div", { className: "three-d-secure-badge" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
		type: "pisell2-shield-01",
		className: "three-d-secure-icon"
	}), /* @__PURE__ */ react.default.createElement("span", { className: "three-d-secure-badge-text" }, "3D")), /* @__PURE__ */ react.default.createElement("span", { className: "three-d-secure-text" }, _pisell_utils.locales.getText("tocPay.text.threeDSecureProtection")))));
};
var Add_default = (0, react.forwardRef)(AddMWCreditCard);
//#endregion
exports.default = Add_default;
