const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../utils/index.js");
const require_index$1 = require("./PayCountdown/index.js");
const require_index$2 = require("./PayAmount/index.js");
const require_serve = require("./PaymentMethods/WalletPass/serve.js");
const require_status = require("./status.js");
const require_utils = require("./utils.js");
const require_index$3 = require("./WalletPassBlock/index.js");
const require_index$4 = require("./PaymentList/index.js");
const require_serve$1 = require("./serve.js");
const require_payGroup = require("./payGroup.js");
const require_model = require("./model.js");
const require_usePaymentLogger = require("../../../hooks/usePaymentLogger.js");
const require_index$5 = require("./PaymentMethods/index.js");
const require_locales = require("./locales.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let decimal_js = require("decimal.js");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/pay/toC/index.tsx
/**
* @title: C端支付
* @description:
* @param {ToCPayProps} props
* @param {any} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 10:43
*/
const ToCPay = (props, ref) => {
	var _state$payGroup, _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4, _dvaStore$core, _dvaStore$core2, _state$payGroup7, _state$payGroup25, _state$order, _state$order2, _state$payGroup30, _state$payGroup31, _state$payGroup32;
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const paymentListRef = (0, react.useRef)();
	const paymentMethodsRef = (0, react.useRef)();
	const [loading, setLoading] = (0, react.useState)(true);
	const [submitLoading, setSubmitLoading] = (0, react.useState)(false);
	const [open, setOpen] = (0, react.useState)(false);
	const [renderType, setRenderType] = (0, react.useState)("modal");
	const walletPassBlockRef = (0, react.useRef)(null);
	const context = require_useEngineContext.default();
	const paymentLogger = require_usePaymentLogger.default({
		orderId: ((_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || "unknown",
		step: "1"
	});
	const [showInformation, setShowInformation] = (0, react.useState)(false);
	const [showDetectIncognitoConfirm, setShowDetectIncognitoConfirm] = (0, react.useState)(false);
	const [showPendingPayment, setShowPendingPayment] = (0, react.useState)(false);
	const utils = context.appHelper.utils || {};
	const dvaStore = (_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getStore) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 ? void 0 : _context$appHelper$ut2.getState();
	const customer_id = (_context$appHelper$ut3 = context.appHelper.utils) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut3.getStore()) === null || _context$appHelper$ut3 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut3.getState) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.call(_context$appHelper$ut3)) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.global) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.userInfo) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.id;
	const { interaction } = utils;
	const shopName = (_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 || (_dvaStore$core = _dvaStore$core.core) === null || _dvaStore$core === void 0 || (_dvaStore$core = _dvaStore$core.shop) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.name;
	_pisell_utils.locales.init(require_locales.default, ((_dvaStore$core2 = dvaStore.core) === null || _dvaStore$core2 === void 0 ? void 0 : _dvaStore$core2.translateLocale) || "en");
	require_index.request.setRequest(utils === null || utils === void 0 ? void 0 : utils.request);
	/**
	* @title: 初始化store
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 14:02
	*/
	const _initStore = (initProps) => {
		var _context$appHelper$ut5;
		dispatch({
			type: "init",
			payload: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, initProps), {}, {
				status: "init",
				payCallback,
				history: (_context$appHelper$ut5 = context.appHelper.utils) === null || _context$appHelper$ut5 === void 0 ? void 0 : _context$appHelper$ut5.history,
				front_order_uuid: (0, _pisell_utils.getUniqueId)("front_order_uuid_")
			})
		});
	};
	const getWalletSetting = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (initProps) {
			var _initProps$order, _utils$translation, _wallet_setting$popup;
			let sale_channel = (initProps === null || initProps === void 0 || (_initProps$order = initProps.order) === null || _initProps$order === void 0 ? void 0 : _initProps$order.sale_channel) || "online-store";
			sale_channel = require_status.saleChannelMap[sale_channel] || sale_channel;
			const { wallet, wallet_setting } = yield require_serve$1.getWalletNameAndPopupStatus({ setting_type: `${sale_channel}-wallet-setting` });
			const setting = {
				name: (utils === null || utils === void 0 || (_utils$translation = utils.translation) === null || _utils$translation === void 0 ? void 0 : _utils$translation.call(utils, wallet === null || wallet === void 0 ? void 0 : wallet.name)) || _pisell_utils.locales.getText("tocPay.text.walletName")(shopName),
				popup_status: (_wallet_setting$popup = wallet_setting === null || wallet_setting === void 0 ? void 0 : wallet_setting.popup_status) !== null && _wallet_setting$popup !== void 0 ? _wallet_setting$popup : 1
			};
			if (setting.popup_status === 1) setTimeout(() => {
				var _walletPassBlockRef$c;
				(_walletPassBlockRef$c = walletPassBlockRef.current) === null || _walletPassBlockRef$c === void 0 || _walletPassBlockRef$c.open();
			});
			dispatch({
				type: "setWalletSetting",
				payload: setting
			});
		});
		return function getWalletSetting(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	/**
	* @title: 全局唯一初始化方法
	* @description:
	* @param {InitProps} initProps
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 14:02
	*/
	const init = function() {
		var _ref2 = require_asyncToGenerator._asyncToGenerator(function* (initProps) {
			const { renderType } = initProps;
			paymentLogger.addLog({
				key: "初始化开始",
				value: ``
			});
			try {
				var _initProps$order2;
				setRenderType(renderType !== null && renderType !== void 0 ? renderType : "modal");
				setOpen(true);
				setSubmitLoading(false);
				setLoading(true);
				const { has_pending } = yield require_serve$1.checkPendingPayment({ order_id: (_initProps$order2 = initProps.order) === null || _initProps$order2 === void 0 ? void 0 : _initProps$order2.order_id });
				if (has_pending) {
					var _initProps$order3;
					paymentLogger.addLog({
						key: "检测到处理中的交易",
						value: `订单${(_initProps$order3 = initProps.order) === null || _initProps$order3 === void 0 ? void 0 : _initProps$order3.order_id} 存在处理中的交易，中断支付流程`
					});
					setLoading(false);
					setShowPendingPayment(true);
					return;
				}
				_initStore(initProps);
				require_payGroup.removePayGroupFromStorage(initProps.order.order_id);
				yield _getPaymentsAndWalletPass(initProps.order);
				yield getWalletSetting(initProps);
				paymentLogger.addLog({
					key: "初始化成功",
					value: ``
				});
			} catch (error) {
				var _initProps$order4;
				paymentLogger.addLog({
					key: "初始化错误",
					value: {
						title: `订单${initProps === null || initProps === void 0 || (_initProps$order4 = initProps.order) === null || _initProps$order4 === void 0 ? void 0 : _initProps$order4.order_id} 初始化失败: ${(error === null || error === void 0 ? void 0 : error.message) || "未知错误"}`,
						data: { order: initProps === null || initProps === void 0 ? void 0 : initProps.order }
					}
				});
				paymentLogger.sendLogs("支付初始化失败", error);
				setOpen(false);
				setLoading(false);
				throw error;
			}
		});
		return function init(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	/**
	* @title: 获取支付列表和钱包
	* @description:
	* @param {any} order
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:51
	*/
	const _getPaymentsAndWalletPass = function() {
		var _ref4 = require_asyncToGenerator._asyncToGenerator(function* (order) {
			paymentLogger.addLog({
				key: "获取支付列表开始",
				value: { title: `订单${order === null || order === void 0 ? void 0 : order.order_id} 开始获取支付列表, 金额: ${order === null || order === void 0 ? void 0 : order.amount}` }
			});
			try {
				var _order$filter_wallet_, _order$_order, _state$payGroup2, _paymentListRef$curre;
				const apiPrefix = require_utils.isMiniProgram(interaction) ? "mini-program/v1.0" : "h5";
				const [data, allList, orderBasicDetail] = yield Promise.all([
					require_serve$1.getPaymentsAndWalletPass({
						order_id: order.order_id,
						total_amount: order.amount,
						sale_channel: order.sale_channel || "online-store",
						filter_wallet_pass_flag: (_order$filter_wallet_ = order === null || order === void 0 ? void 0 : order.filter_wallet_pass_flag) !== null && _order$filter_wallet_ !== void 0 ? _order$filter_wallet_ : 0
					}, apiPrefix),
					require_serve.getMachineCodeList({
						order_id: order.order_id,
						available: 2,
						prepare_payments: [],
						filter_prepare_wallet_pass: 1,
						sale_channel: ((_order$_order = order._order) === null || _order$_order === void 0 ? void 0 : _order$_order.sale_channel) || "online-store",
						other_exact_codes: []
					}),
					require_serve$1.getOrderBasicDetail(order.order_id)
				]);
				const { remain_amount = 0, wallet_pass_recommend_list, wallet_pass_amount, payment_list, walletPass } = data || {};
				const products = (orderBasicDetail.detail || []).map((item) => {
					var _ref3;
					return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
						is_price_include_tax: orderBasicDetail.is_price_include_tax,
						product_bundle: (_ref3 = item.product_bundle || []) === null || _ref3 === void 0 ? void 0 : _ref3.map((bundle) => {
							return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, bundle), {}, { is_price_include_tax: orderBasicDetail.is_price_include_tax });
						})
					});
				});
				let res = {};
				let withCustomerIdList = [];
				let waitAmount = remain_amount;
				let recommended, transformList, noApplicableVoucher, recommendedAmount;
				if (walletPass) {
					res = utils.walletPassEvaluator.getRecommendedVouchers({
						orderTotalAmount: Math.min(orderBasicDetail.product_unpaid_total_pay_amount, order.amount),
						products,
						vouchers: allList
					});
					recommended = res === null || res === void 0 ? void 0 : res.recommended;
					transformList = res === null || res === void 0 ? void 0 : res.transformList;
					noApplicableVoucher = res === null || res === void 0 ? void 0 : res.noApplicableVoucher;
					recommendedAmount = res === null || res === void 0 ? void 0 : res.recommendedAmount;
					paymentLogger.addLog({
						key: "支付数据获取",
						value: ""
					});
					withCustomerIdList = (recommended || []).map((item) => {
						return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, {
							customer_id: customer_id || item.customer_id,
							metadata: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item.metadata || {}), {}, { unique_payment_number: (0, _pisell_utils.getUniqueId)() })
						});
					});
					waitAmount = new decimal_js.Decimal((order === null || order === void 0 ? void 0 : order.amount) || 0).minus(new decimal_js.Decimal(recommendedAmount)).toNumber();
				}
				dispatch({
					type: "setPayGroup",
					payload: {
						order_id: order === null || order === void 0 ? void 0 : order.order_id,
						orderDetail: orderBasicDetail,
						_order: order,
						payment_group_id: order === null || order === void 0 ? void 0 : order.payment_group_id,
						walletPass: walletPass ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.walletPass), walletPass), {}, {
							amount: wallet_pass_amount,
							cards: withCustomerIdList,
							amount: recommendedAmount,
							noApplicableVoucher,
							transformList
						}) : null,
						total_amount: order === null || order === void 0 ? void 0 : order.amount,
						wait_amount: Math.max(0, waitAmount)
					}
				});
				yield (_paymentListRef$curre = paymentListRef.current) === null || _paymentListRef$curre === void 0 ? void 0 : _paymentListRef$curre.init(payment_list, remain_amount || 0);
				setTimeout(() => {
					setLoading(false);
				}, 100);
				paymentLogger.addLog({
					key: "获取支付列表成功",
					value: ""
				});
			} catch (err) {
				console.log("err", err);
				paymentLogger.addLog({
					key: "获取支付列表错误",
					value: {
						title: `订单${order === null || order === void 0 ? void 0 : order.order_id} 获取失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: { order }
					}
				});
				paymentLogger.sendLogs("获取支付列表失败", err);
				payCallback({
					key: "fail",
					data: {
						err,
						order,
						fn: "_getPaymentsAndWalletPass"
					}
				});
			}
		});
		return function _getPaymentsAndWalletPass(_x3) {
			return _ref4.apply(this, arguments);
		};
	}();
	/**
	* @title: 选择支付方式
	* @description:
	* @param {string} type
	* @param {PayGroup} payGroup
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:46
	*/
	const handlePaymentByType = function() {
		var _ref5 = require_asyncToGenerator._asyncToGenerator(function* (type, payGroup) {
			var _state$payGroup3;
			const paymentType = type || ((_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 || (_state$payGroup3 = _state$payGroup3.otherPayment) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.service);
			const currentPayGroup = payGroup || state.payGroup;
			paymentLogger.addLog({
				key: "开始支付处理",
				value: ""
			});
			try {
				var _paymentMethodsRef$cu;
				setSubmitLoading(true);
				yield (_paymentMethodsRef$cu = paymentMethodsRef.current) === null || _paymentMethodsRef$cu === void 0 ? void 0 : _paymentMethodsRef$cu.onPay({
					type: paymentType,
					payGroup: currentPayGroup
				});
				paymentLogger.addLog({
					key: "支付方法调用成功",
					value: ""
				});
				paymentLogger.sendLogs("支付方法调用成功");
				setSubmitLoading(false);
			} catch (error) {
				paymentLogger.addLog({
					key: "支付处理错误",
					value: {
						title: `订单${currentPayGroup === null || currentPayGroup === void 0 ? void 0 : currentPayGroup.order_id} ${paymentType}支付失败: ${(error === null || error === void 0 ? void 0 : error.message) || "未知错误"}`,
						data: {
							type: paymentType,
							payGroup: currentPayGroup
						}
					}
				});
				paymentLogger.sendLogs("支付处理失败", error);
				setSubmitLoading(false);
				throw error;
			}
		});
		return function handlePaymentByType(_x4, _x5) {
			return _ref5.apply(this, arguments);
		};
	}();
	/**
	* @title: 创建交易组
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:41
	*/
	const paymentInit = function() {
		var _ref6 = require_asyncToGenerator._asyncToGenerator(function* (newOrderId) {
			var _state$payGroup4;
			const orderId = newOrderId || ((_state$payGroup4 = state.payGroup) === null || _state$payGroup4 === void 0 ? void 0 : _state$payGroup4.order_id);
			paymentLogger.addLog({
				key: "开始创建交易组",
				value: ""
			});
			setSubmitLoading(true);
			try {
				var _state$payGroup5;
				const data = yield require_serve$1.getPaymentInit({
					order_id: orderId,
					amount: (_state$payGroup5 = state.payGroup) === null || _state$payGroup5 === void 0 ? void 0 : _state$payGroup5.total_amount
				});
				dispatch({
					type: "setPayGroup",
					payload: {
						order_id: orderId,
						total_amount: data.amount,
						payment_group_id: data.payment_group_id,
						payment_priority: data.payment_priority
					}
				});
				paymentLogger.addLog({
					key: "创建交易组成功",
					value: ""
				});
				return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, state.payGroup), {}, {
					order_id: orderId,
					total_amount: data.amount,
					payment_group_id: data.payment_group_id,
					payment_priority: data.payment_priority
				});
			} catch (err) {
				paymentLogger.addLog({
					key: "创建交易组错误",
					value: {
						title: `订单${orderId} 创建交易组失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: { order: orderId }
					}
				});
				paymentLogger.sendLogs("创建交易组失败", err);
				setSubmitLoading(false);
				payCallback({
					key: "fail",
					data: {
						err,
						orderId,
						fn: "paymentInit"
					}
				});
				throw err;
			}
		});
		return function paymentInit(_x6) {
			return _ref6.apply(this, arguments);
		};
	}();
	/**
	* @title: 除去钱包后的待支付金额
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:47
	*/
	const waitAmount = (0, react.useMemo)(() => {
		var _state$payGroup6;
		return ((_state$payGroup6 = state.payGroup) === null || _state$payGroup6 === void 0 ? void 0 : _state$payGroup6.wait_amount) || 0;
	}, [(_state$payGroup7 = state.payGroup) === null || _state$payGroup7 === void 0 ? void 0 : _state$payGroup7.wait_amount]);
	/**
	* @title: 是否仅钱包支付
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:46
	*/
	const isWalletPassPay = (0, react.useMemo)(() => {
		var _state$payGroup8;
		const isWalletPass = (_state$payGroup8 = state.payGroup) === null || _state$payGroup8 === void 0 || (_state$payGroup8 = _state$payGroup8.walletPass) === null || _state$payGroup8 === void 0 || (_state$payGroup8 = _state$payGroup8.cards) === null || _state$payGroup8 === void 0 ? void 0 : _state$payGroup8.length;
		return waitAmount <= 0 && isWalletPass;
	}, [state.payGroup, waitAmount]);
	/**
	* @title: 是否禁止提交
	* @description:
	* @param {*} useMemo
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:40
	*/
	const isDisabled = (0, react.useMemo)(() => {
		if (waitAmount > 0) {
			var _state$payGroup9;
			return !((_state$payGroup9 = state.payGroup) === null || _state$payGroup9 === void 0 || (_state$payGroup9 = _state$payGroup9.otherPayment) === null || _state$payGroup9 === void 0 ? void 0 : _state$payGroup9.service);
		} else return !isWalletPassPay;
	}, [
		state.payGroup,
		waitAmount,
		isWalletPassPay
	]);
	/**
	* @title: 获取是否提示无痕模式
	* @description: walletPass和三方支付的组合支付在无痕模式浏览器需要给出提示
	* @return: *
	* @Author: shengjie.zuo
	* @Date: 2024-08-30 10:23:48
	*/
	const getIsShowDetectIncognitoConfirm = function() {
		var _ref7 = require_asyncToGenerator._asyncToGenerator(function* (payGroup) {
			const { isPrivate } = yield (0, _pisell_utils.detectIncognito)();
			return ((payGroup === null || payGroup === void 0 ? void 0 : payGroup.wait_amount) || 0) > 0 && require_utils.getIsWalletPassAndTripartitePayment(payGroup) && isPrivate;
		});
		return function getIsShowDetectIncognitoConfirm(_x7) {
			return _ref7.apply(this, arguments);
		};
	}();
	/**
	* @title: 提交
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:25
	*/
	const onSubmit = function() {
		var _ref8 = require_asyncToGenerator._asyncToGenerator(function* (newOrderId) {
			var _state$payGroup10, _state$payGroup11, _state$payGroup12;
			paymentLogger.addLog({
				key: "开始提交支付",
				value: `订单${newOrderId || ((_state$payGroup10 = state.payGroup) === null || _state$payGroup10 === void 0 ? void 0 : _state$payGroup10.order_id)} 开始提交, 待支付: ${(_state$payGroup11 = state.payGroup) === null || _state$payGroup11 === void 0 ? void 0 : _state$payGroup11.wait_amount}, 总金额: ${(_state$payGroup12 = state.payGroup) === null || _state$payGroup12 === void 0 ? void 0 : _state$payGroup12.total_amount}`
			});
			let newPayGroup = state.payGroup;
			try {
				var _state$payGroup13, _state$payGroup14;
				if (yield getIsShowDetectIncognitoConfirm(newPayGroup)) {
					paymentLogger.addLog({
						key: "无痕模式检测",
						value: ""
					});
					setShowDetectIncognitoConfirm(true);
					return;
				}
				if (!((_state$payGroup13 = state.payGroup) === null || _state$payGroup13 === void 0 ? void 0 : _state$payGroup13.payment_group_id)) {
					paymentLogger.addLog({
						key: "需要创建交易组",
						value: ""
					});
					newPayGroup = yield paymentInit(newOrderId);
				}
				paymentLogger.addLog({
					key: "调用支付方法",
					value: `订单${newPayGroup === null || newPayGroup === void 0 ? void 0 : newPayGroup.order_id} 交易组${newPayGroup === null || newPayGroup === void 0 ? void 0 : newPayGroup.payment_group_id} 使用${(_state$payGroup14 = state.payGroup) === null || _state$payGroup14 === void 0 || (_state$payGroup14 = _state$payGroup14.otherPayment) === null || _state$payGroup14 === void 0 ? void 0 : _state$payGroup14.service}支付`
				});
				handlePaymentByType(void 0, newPayGroup);
				paymentLogger.addLog({
					key: "支付提交调用成功",
					value: ""
				});
			} catch (err) {
				var _state$payGroup15;
				console.log("err", err);
				paymentLogger.addLog({
					key: "提交支付错误",
					value: {
						title: `订单${newOrderId || ((_state$payGroup15 = state.payGroup) === null || _state$payGroup15 === void 0 ? void 0 : _state$payGroup15.order_id)} 提交失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: { newPayGroup }
					}
				});
				paymentLogger.sendLogs("提交支付失败", err);
			}
		});
		return function onSubmit(_x8) {
			return _ref8.apply(this, arguments);
		};
	}();
	/**
	* @title: 支付方式改变时
	* @description:
	* @param {any} payload
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-15 13:51
	*/
	const paymentTypeChange = (payload) => {
		dispatch({
			type: "setPayGroup",
			payload: { otherPayment: payload }
		});
	};
	const okText = (0, react.useMemo)(() => {
		var _state$payGroup16, _state$payGroup17;
		let current = (_state$payGroup16 = state.payGroup) === null || _state$payGroup16 === void 0 ? void 0 : _state$payGroup16.otherPayment;
		if (((_state$payGroup17 = state.payGroup) === null || _state$payGroup17 === void 0 ? void 0 : _state$payGroup17.wait_amount) == 0) return _pisell_utils.locales.getText("tocPay.text.submit");
		if (current === null || current === void 0 ? void 0 : current.surcharge) {
			var _state$payGroup18;
			return /* @__PURE__ */ react.default.createElement("span", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup18 = state.payGroup) === null || _state$payGroup18 === void 0 ? void 0 : _state$payGroup18.wait_amount) || 0 }), " +", _pisell_utils.locales.getText("tocPay.text.surcharge"));
		} else {
			var _state$payGroup19;
			return /* @__PURE__ */ react.default.createElement("span", { className: "" }, _pisell_utils.locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, { value: ((_state$payGroup19 = state.payGroup) === null || _state$payGroup19 === void 0 ? void 0 : _state$payGroup19.wait_amount) || 0 }));
		}
	}, [state.payGroup]);
	/**
	* @title: 支付结果回调
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-09 14:25
	*/
	const payCallback = function() {
		var _ref9 = require_asyncToGenerator._asyncToGenerator(function* (res) {
			var _res$data, _state$payGroup20, _props$callback;
			const orderId = (res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.order_id) || ((_state$payGroup20 = state.payGroup) === null || _state$payGroup20 === void 0 ? void 0 : _state$payGroup20.order_id);
			if (res.key === "success") {
				paymentLogger.addLog({
					key: "支付最终成功",
					value: `订单${orderId} 支付成功`
				});
				paymentLogger.sendLogs("支付成功");
			} else if (res.key === "fail") {
				var _res$data2;
				paymentLogger.addLog({
					key: "支付最终失败",
					value: {
						title: `订单${orderId} 支付失败`,
						data: res === null || res === void 0 ? void 0 : res.data
					}
				});
				paymentLogger.sendLogs("支付失败", res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.err);
			} else if (res.key === "cancel") {
				paymentLogger.addLog({
					key: "支付被取消",
					value: `订单${orderId} 支付被取消`
				});
				setOpen(false);
			}
			(_props$callback = props.callback) === null || _props$callback === void 0 || _props$callback.call(props, res);
		});
		return function payCallback(_x9) {
			return _ref9.apply(this, arguments);
		};
	}();
	/**
	* @title: 取消支付
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-08 16:34
	*/
	const onCancel = () => {
		var _state$payGroup21, _state$payGroup22, _state$payGroup23;
		paymentLogger.addLog({
			key: "用户主动取消支付",
			value: `订单${(_state$payGroup21 = state.payGroup) === null || _state$payGroup21 === void 0 ? void 0 : _state$payGroup21.order_id} 用户取消支付`
		});
		require_payGroup.removePayGroupFromStorage((_state$payGroup22 = state.payGroup) === null || _state$payGroup22 === void 0 ? void 0 : _state$payGroup22.order_id);
		setShowInformation(false);
		setOpen(false);
		payCallback({
			key: "cancel",
			data: { order_id: (_state$payGroup23 = state.payGroup) === null || _state$payGroup23 === void 0 ? void 0 : _state$payGroup23.order_id }
		});
	};
	(0, ahooks.useUpdateEffect)(() => {
		var _paymentListRef$curre2, _state$payGroup24;
		(_paymentListRef$curre2 = paymentListRef.current) === null || _paymentListRef$curre2 === void 0 || _paymentListRef$curre2.calculateSurcharge(((_state$payGroup24 = state.payGroup) === null || _state$payGroup24 === void 0 ? void 0 : _state$payGroup24.wait_amount) || 0);
	}, [(_state$payGroup25 = state.payGroup) === null || _state$payGroup25 === void 0 ? void 0 : _state$payGroup25.wait_amount]);
	(0, react.useImperativeHandle)(ref, () => {
		return {
			init,
			payCallback,
			onSubmit
		};
	});
	/**
	* @title: 自定义渲染支付列表的标题
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:49
	*/
	const renderPaymentListTitle = () => {
		var _state$payGroup26;
		if (waitAmount == 0) return /* @__PURE__ */ react.default.createElement("div", { className: "custom-payment-list-title" }, /* @__PURE__ */ react.default.createElement("div", { className: "" }, _pisell_utils.locales.getText("tocPay.text.clickSubmitToComplete")), /* @__PURE__ */ react.default.createElement("div", { className: "custom-payment-list-title-desc" }, _pisell_utils.locales.getText("tocPay.text.noExtraPayNeeded")));
		if (((_state$payGroup26 = state.payGroup) === null || _state$payGroup26 === void 0 ? void 0 : _state$payGroup26.total_amount) !== waitAmount) {
			var _state$payGroup27;
			return /* @__PURE__ */ react.default.createElement("div", { className: "custom-payment-list-title" }, _pisell_utils.locales.getText("tocPay.text.continuePayment"), " ", /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellText.Amount, {
				className: "custom-payment-list-title-price",
				value: ((_state$payGroup27 = state.payGroup) === null || _state$payGroup27 === void 0 ? void 0 : _state$payGroup27.wait_amount) || 0
			}));
		}
		return _pisell_utils.locales.getText("tocPay.text.paymentMethod");
	};
	/**
	* @title: 钱包变化
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:50
	*/
	const onWalletPassChange = (0, ahooks.useMemoizedFn)((walletPass) => {
		var _state$payGroup28, _state$payGroup29;
		const _waitAmount = new decimal_js.Decimal(String(((_state$payGroup28 = state.payGroup) === null || _state$payGroup28 === void 0 ? void 0 : _state$payGroup28.total_amount) || 0)).minus(new decimal_js.Decimal(String(walletPass.amount || 0))).toNumber();
		dispatch({
			type: "setPayGroup",
			payload: {
				walletPass: require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, (_state$payGroup29 = state.payGroup) === null || _state$payGroup29 === void 0 ? void 0 : _state$payGroup29.walletPass), walletPass),
				wait_amount: _waitAmount
			}
		});
	});
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellContainer, {
		renderType,
		props: {
			forceRender: true,
			title: /* @__PURE__ */ react.default.createElement("div", null, _pisell_utils.locales.getText("tocPay.text.checkout")),
			open,
			cancelVisible: false,
			okText,
			wrapClassName: "toc-pay-modal-content",
			bodyStyle: { padding: 24 },
			onCancel: () => {
				setShowInformation(true);
			},
			okButtonProps: {
				disabled: isDisabled,
				loading: submitLoading,
				className: "toc-pay-submit-btn"
			},
			onOk: () => {
				onSubmit();
			}
		}
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal.Information, {
		open: showInformation,
		title: _pisell_utils.locales.getText("tocPay.text.prompt"),
		describe: _pisell_utils.locales.getText("tocPay.text.confirmLeave"),
		okText: _pisell_utils.locales.getText("tocPay.text.continuePay"),
		cancelText: _pisell_utils.locales.getText("tocPay.text.leave"),
		closable: true,
		cancelButtonProps: { onClick: () => {
			onCancel();
		} },
		maskClosable: false,
		onCancel: () => {
			setShowInformation(false);
		},
		onOk: () => {
			setShowInformation(false);
		}
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal.Information, {
		open: showPendingPayment,
		describe: _pisell_utils.locales.getText("tocPay.text.pendingPaymentDesc"),
		okText: _pisell_utils.locales.getText("tocPay.text.pendingPaymentOk"),
		cancelVisible: false,
		closable: false,
		maskClosable: false,
		onOk: () => {
			paymentLogger.sendLogs("此订单有一笔支付正在处理中");
			setShowPendingPayment(false);
			setOpen(false);
		}
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal.Information, {
		open: showDetectIncognitoConfirm,
		describe: _pisell_utils.locales.getText("tocPay.text.detectIncognitoConfirm"),
		okText: _pisell_utils.locales.getText("tocPay.text.copyLink"),
		closable: true,
		maskClosable: false,
		onCancel: () => {
			setShowDetectIncognitoConfirm(false);
		},
		onOk: require_asyncToGenerator._asyncToGenerator(function* () {
			yield (0, _pisell_utils.copyToClipboard)(window.location.href);
			(0, _pisell_materials.PisellToast)({ content: _pisell_utils.locales.getText("tocPay.text.copySuccess") });
		})
	}), /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellLoading, {
		loading,
		mask: true
	}, ((_state$order = state.order) === null || _state$order === void 0 ? void 0 : _state$order.countdown) && /* @__PURE__ */ react.default.createElement(require_index$1.default, { time: (_state$order2 = state.order) === null || _state$order2 === void 0 ? void 0 : _state$order2.countdown }), /* @__PURE__ */ react.default.createElement(require_index$2.default, { amount: ((_state$payGroup30 = state.payGroup) === null || _state$payGroup30 === void 0 ? void 0 : _state$payGroup30.total_amount) || 0 }), ((_state$payGroup31 = state.payGroup) === null || _state$payGroup31 === void 0 ? void 0 : _state$payGroup31.walletPass) && /* @__PURE__ */ react.default.createElement(require_index$3.default, {
		customer_id,
		payGroup: state.payGroup,
		onWalletPassChange,
		ref: walletPassBlockRef,
		walletSetting: state.walletSetting
	}), /* @__PURE__ */ react.default.createElement(require_index$4.default, {
		ref: paymentListRef,
		handlePayment: (detail) => {
			handlePaymentByType(detail === null || detail === void 0 ? void 0 : detail.service);
		},
		paymentTypeChange,
		hide: ((_state$payGroup32 = state.payGroup) === null || _state$payGroup32 === void 0 ? void 0 : _state$payGroup32.wait_amount) == 0,
		renderTitle: renderPaymentListTitle
	})), /* @__PURE__ */ react.default.createElement(require_index$5.default, {
		ref: paymentMethodsRef,
		payCallback
	}));
};
var toC_default = require_model.Provider((0, react.forwardRef)(ToCPay));
//#endregion
exports.default = toC_default;
