const require_runtime = require("../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../hooks/useEngineContext.js");
const require_utils = require("./utils.js");
const require_serve = require("./serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
require("./index.less");
//#region src/components/pay/toC/PaymentList/index.tsx
const offline = [
	"pisell.offline_payment",
	"pisell.other.cashmanual",
	"pisell.custom_payment"
];
/**
* @title: 支付列表
* @description:
* @param {PaymentListProps} props
* @param {any} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 10:52
*/
const PaymentList = (props, ref) => {
	var _context$appHelper, _props$store2, _props$store4;
	const [paymentList, setPaymentList] = (0, react.useState)([]);
	const [paymentType, setPaymentType] = (0, react.useState)("");
	const [loading, setLoading] = (0, react.useState)(true);
	const context = require_useEngineContext.default();
	const { interaction } = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils;
	const _paymentList = (0, react.useMemo)(() => {
		var _props$store;
		return (props === null || props === void 0 || (_props$store = props.store) === null || _props$store === void 0 ? void 0 : _props$store.paymentList) || paymentList;
	}, [paymentList, props === null || props === void 0 || (_props$store2 = props.store) === null || _props$store2 === void 0 ? void 0 : _props$store2.paymentList]);
	const _paymentType = (0, react.useMemo)(() => {
		var _props$store3;
		return (props === null || props === void 0 || (_props$store3 = props.store) === null || _props$store3 === void 0 ? void 0 : _props$store3.paymentType) || paymentType;
	}, [paymentType, props === null || props === void 0 || (_props$store4 = props.store) === null || _props$store4 === void 0 ? void 0 : _props$store4.paymentType]);
	(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (initProps) {
			var _props$paymentTypeCha;
			setLoading(true);
			const res = yield require_serve.getPaymentList({
				order_id: initProps === null || initProps === void 0 ? void 0 : initProps.order.order_id,
				total_amount: initProps === null || initProps === void 0 ? void 0 : initProps.order.total_amount,
				sale_channel: (initProps === null || initProps === void 0 ? void 0 : initProps.order.sale_channel) || "online-store"
			});
			const paymentList = res.paymentList;
			let first = paymentList[0];
			(_props$paymentTypeCha = props.paymentTypeChange) === null || _props$paymentTypeCha === void 0 || _props$paymentTypeCha.call(props, first);
			setPaymentList(paymentList);
			setPaymentType(first.service);
			isAutoPayment(paymentList);
			setLoading(false);
			return res;
		});
		return function _getPaymentList(_x) {
			return _ref.apply(this, arguments);
		};
	})();
	/**
	* @title: 计算手续费
	* @description:
	* @param {number} amount
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:52
	*/
	const _calculateSurcharge = (amount) => {
		if (!paymentList.length) return;
		let list = require_utils.calculateSurcharge(paymentList, amount);
		setPaymentList(list);
		return list;
	};
	const isApp = (0, react.useMemo)(() => {
		var _interaction$utils, _interaction$utils$is;
		return interaction === null || interaction === void 0 || (_interaction$utils = interaction.utils) === null || _interaction$utils === void 0 || (_interaction$utils$is = _interaction$utils.isAppEnv) === null || _interaction$utils$is === void 0 ? void 0 : _interaction$utils$is.call(_interaction$utils);
	}, []);
	/**
	* @title: 唯一初始化
	* @description:
	* @param {any} list
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:53
	*/
	const init = (list, amount) => {
		var _context$appHelper2;
		setLoading(true);
		const { paymentList, walletPass } = require_serve.filterPaymentList(list, ((_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.constants) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.platform) || "h5", isApp, amount);
		if (paymentList.length > 0) {
			let first = paymentList[0];
			setPaymentList(paymentList);
			paymentTypeChange(first.service, first);
			isAutoPayment(paymentList, walletPass);
		}
		setLoading(false);
		return paymentList;
	};
	/**
	* @title: 当只有线下支付时, 自动触发支付
	* @description:
	* @param {any} list
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:53
	*/
	const isAutoPayment = (list, walletPass) => {
		const _list = list;
		if (walletPass) return;
		if (_list.length === 1) {
			let isOfflinePayment = _list.find((d) => offline.find((t) => d.service.includes(t)));
			if (isOfflinePayment) {
				setTimeout(() => {
					var _props$handlePayment;
					(_props$handlePayment = props.handlePayment) === null || _props$handlePayment === void 0 || _props$handlePayment.call(props, isOfflinePayment);
				}, 200);
				return true;
			}
		}
		return false;
	};
	/**
	* @title: 支付方式变化时回调
	* @description:
	* @param {any} key
	* @param {any} item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:54
	*/
	const paymentTypeChange = (key, item) => {
		var _props$paymentTypeCha2;
		setPaymentType(key);
		(_props$paymentTypeCha2 = props.paymentTypeChange) === null || _props$paymentTypeCha2 === void 0 || _props$paymentTypeCha2.call(props, item);
	};
	(0, react.useImperativeHandle)(ref, () => ({
		init,
		calculateSurcharge: _calculateSurcharge
	}));
	/**
	* @title: 渲染手续费弹窗
	* @description:
	* @param {any} _item
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:54
	*/
	const renderToast = (_item) => {
		var _item$params, _item$params2, _item$params3;
		return /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-wrap" }, /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-title" }, _item.title + " " + ((_item === null || _item === void 0 || (_item$params = _item.params) === null || _item$params === void 0 ? void 0 : _item$params.description) ? _pisell_utils.locales.getText("tocPay.text.paymentInstructions") : _pisell_utils.locales.getText("tocPay.text.serviceCharge"))), /* @__PURE__ */ react.default.createElement("div", {
			className: "surcharge-info",
			dangerouslySetInnerHTML: { __html: (_item === null || _item === void 0 || (_item$params2 = _item.params) === null || _item$params2 === void 0 ? void 0 : _item$params2.description) ? _item === null || _item === void 0 || (_item$params3 = _item.params) === null || _item$params3 === void 0 ? void 0 : _item$params3.description : _item.surcharge_fee }
		}));
	};
	/**
	* @description: 渲染信用卡手续费弹窗
	* @param {any} _item
	* @return {*}
	*/
	const renderCreditCardToast = (_item) => {
		var _item$surcharge_confi;
		return /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-wrap surcharge-modal-wrap-credit-card" }, /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-lists" }, _item === null || _item === void 0 || (_item$surcharge_confi = _item.surcharge_config_result) === null || _item$surcharge_confi === void 0 ? void 0 : _item$surcharge_confi.map((d) => {
			var _d$icons, _d$lists;
			return /* @__PURE__ */ react.default.createElement("div", {
				key: d.name,
				className: "surcharge-modal-list-item"
			}, /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-list-item-header" }, d === null || d === void 0 || (_d$icons = d.icons) === null || _d$icons === void 0 ? void 0 : _d$icons.map((icon) => {
				return /* @__PURE__ */ react.default.createElement(_pisell_materials.Image, {
					src: icon,
					className: "header-icon",
					isPreview: false
				});
			}), /* @__PURE__ */ react.default.createElement("span", null, d.name)), /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-modal-list-item-content" }, d === null || d === void 0 || (_d$lists = d.lists) === null || _d$lists === void 0 ? void 0 : _d$lists.map((item) => {
				return /* @__PURE__ */ react.default.createElement("div", {
					key: item.name,
					className: "content-item"
				}, /* @__PURE__ */ react.default.createElement("span", null, _pisell_utils.locales.getText(item.area_text)), /* @__PURE__ */ react.default.createElement("span", { className: "item-fee" }, item.surcharge_fee), /* @__PURE__ */ react.default.createElement("span", null, item.surcharge_text));
			})));
		})));
	};
	/**
	* @title: 列表
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:55
	*/
	const content = (0, react.useMemo)(() => {
		if (loading) return null;
		if (!_paymentList.length) return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellEmpty, { description: _pisell_utils.locales.getText("tocPay.text.noAvailablePaymentMethods") });
		return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellCheckboxGroup, {
			options: _paymentList,
			value: _paymentType,
			iconPosition: "right",
			mode: "single",
			rowKey: "service",
			className: "payment-list",
			gap: 12,
			onChange: paymentTypeChange,
			optionClassName: "payment-item",
			renderOption: (d, active) => {
				var _d$params, _d$surcharge_config_r, _d$params2;
				const showIcon = d.surcharge && d.service !== "pisell.other.cashmanual" && d.service !== "pisell.custom_payment" || (d === null || d === void 0 || (_d$params = d.params) === null || _d$params === void 0 ? void 0 : _d$params.description) && (d.service === "pisell.other.cashmanual" || d.service === "pisell.custom_payment");
				const showCreditCardToast = (d === null || d === void 0 ? void 0 : d.service) === "mw.credit_card" && (d === null || d === void 0 || (_d$surcharge_config_r = d.surcharge_config_result) === null || _d$surcharge_config_r === void 0 ? void 0 : _d$surcharge_config_r.length) > 0;
				return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellRow, {
					platform: "h5",
					key: d.service,
					gap: 12,
					centerContentFill: true,
					leftContent: /* @__PURE__ */ react.default.createElement("img", {
						className: "payment-type-image",
						src: d.url
					}),
					centerContent: /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-name", { "text-active-color": active }) }, /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("payment-name-text", { "text-active-color": active }) }, d.title, " "), /* @__PURE__ */ react.default.createElement("div", { className: "" }, ((d === null || d === void 0 || (_d$params2 = d.params) === null || _d$params2 === void 0 ? void 0 : _d$params2.description) || d.surcharge) && d.service !== "pisell.offline_payment" ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellTooltip, {
						title: showCreditCardToast ? renderCreditCardToast(d) : renderToast(d),
						onClick: (e) => {
							e.stopPropagation();
						},
						overlayClassName: showCreditCardToast ? "surcharge-tooltip-overlay-wrap" : "",
						getPopupContainer: (triggerNode) => {
							if (showCreditCardToast) {
								var _triggerNode$closest;
								return (triggerNode === null || triggerNode === void 0 || (_triggerNode$closest = triggerNode.closest) === null || _triggerNode$closest === void 0 ? void 0 : _triggerNode$closest.call(triggerNode, ".toc-pay-modal-content .pisell-lowcode-modal")) || document.body;
							}
							return document.body;
						}
					}, /* @__PURE__ */ react.default.createElement("span", { className: (0, classnames.default)("surcharge-wrap", { "text-active-color": active }) }, d.surcharge ? /* @__PURE__ */ react.default.createElement("span", { className: (0, classnames.default)("surcharge", { "text-active-color": active }) }, d.surcharge, " ") : null, showIcon ? /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
						type: "pisell2-help-circle",
						className: (0, classnames.default)("icon", { "text-active-color": active })
					}) : null)) : null, d.payment_method && d.service === "pisell.offline_payment" ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellTooltip, {
						title: renderToast(d),
						onClick: (e) => {
							e.stopPropagation();
						}
					}, /* @__PURE__ */ react.default.createElement("div", { className: "surcharge-wrap" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Iconfont, {
						type: "pisell2-help-circle",
						className: (0, classnames.default)("icon", { "text-active-color": active })
					}))) : null))
				});
			}
		});
	}, [
		_paymentList,
		_paymentType,
		loading
	]);
	return props.hide ? null : /* @__PURE__ */ react.default.createElement("div", { className: "" }, /* @__PURE__ */ react.default.createElement("div", { className: "payment-header" }, props.renderTitle ? props.renderTitle() : _pisell_utils.locales.getText("tocPay.text.paymentMethod")), content);
};
var PaymentList_default = (0, react.forwardRef)(PaymentList);
//#endregion
exports.default = PaymentList_default;
