import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { request } from "../../../utils/index.js";
import PayCountdown from "./PayCountdown/index.js";
import PayAmount from "./PayAmount/index.js";
import { getMachineCodeList } from "./PaymentMethods/WalletPass/serve.js";
import { saleChannelMap } from "./status.js";
import { getIsWalletPassAndTripartitePayment } from "./utils.js";
import WalletPassBlock_default from "./WalletPassBlock/index.js";
import PaymentList_default from "./PaymentList/index.js";
import { checkPendingPayment, getOrderBasicDetail, getPaymentInit, getPaymentsAndWalletPass, getWalletNameAndPopupStatus } from "./serve.js";
import { removePayGroupFromStorage } from "./payGroup.js";
import { Context, Provider } from "./model.js";
import usePaymentLogger from "../../../hooks/usePaymentLogger.js";
import PaymentMethods_default from "./PaymentMethods/index.js";
import locales_default from "./locales.js";
import React, { forwardRef, useContext, useImperativeHandle, useMemo, useRef, useState } from "react";
import { copyToClipboard, detectIncognito, getUniqueId, locales } from "@pisell/utils";
import { Decimal as Decimal$1 } from "decimal.js";
import { PisellContainer, PisellLoading, PisellModal, PisellText, PisellToast } from "@pisell/materials";
import { useMemoizedFn, useUpdateEffect } from "ahooks";
import "./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 } = useContext(Context);
	const paymentListRef = useRef();
	const paymentMethodsRef = useRef();
	const [loading, setLoading] = useState(true);
	const [submitLoading, setSubmitLoading] = useState(false);
	const [open, setOpen] = useState(false);
	const [renderType, setRenderType] = useState("modal");
	const walletPassBlockRef = useRef(null);
	const context = useEngineContext();
	const paymentLogger = usePaymentLogger({
		orderId: ((_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || "unknown",
		step: "1"
	});
	const [showInformation, setShowInformation] = useState(false);
	const [showDetectIncognitoConfirm, setShowDetectIncognitoConfirm] = useState(false);
	const [showPendingPayment, setShowPendingPayment] = 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 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;
	locales.init(locales_default, ((_dvaStore$core2 = dvaStore.core) === null || _dvaStore$core2 === void 0 ? void 0 : _dvaStore$core2.translateLocale) || "en");
	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: _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: getUniqueId("front_order_uuid_")
			})
		});
	};
	const getWalletSetting = function() {
		var _ref = _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 = saleChannelMap[sale_channel] || sale_channel;
			const { wallet, wallet_setting } = yield 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)) || 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 = _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 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);
				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 = _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$_order, _state$payGroup2, _paymentListRef$curre;
				const [data, allList, orderBasicDetail] = yield Promise.all([
					getPaymentsAndWalletPass({
						order_id: order.order_id,
						total_amount: order.amount,
						sale_channel: order.sale_channel || "online-store"
					}),
					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: []
					}),
					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 _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 _objectSpread2(_objectSpread2({}, bundle), {}, { is_price_include_tax: orderBasicDetail.is_price_include_tax });
						})
					});
				});
				const { recommended, transformList, noApplicableVoucher, recommendedAmount } = utils.walletPassEvaluator.getRecommendedVouchers({
					orderTotalAmount: Math.min(orderBasicDetail.product_unpaid_total_pay_amount, order.amount),
					products,
					vouchers: allList || []
				});
				paymentLogger.addLog({
					key: "支付数据获取",
					value: ""
				});
				(wallet_pass_recommend_list || []).map((item) => {
					return _objectSpread2(_objectSpread2({}, item), {}, {
						customer_id: customer_id || item.customer_id,
						metadata: _objectSpread2(_objectSpread2({}, item.metadata || {}), {}, { unique_payment_number: getUniqueId() })
					});
				});
				const waitAmount = new Decimal$1((order === null || order === void 0 ? void 0 : order.amount) || 0).minus(new Decimal$1(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 ? _objectSpread2(_objectSpread2(_objectSpread2({}, (_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.walletPass), walletPass), {}, {
							cards: recommended,
							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 = _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 = _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 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 _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 = 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 = 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 = 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 = _asyncToGenerator(function* (payGroup) {
			const { isPrivate } = yield detectIncognito();
			return ((payGroup === null || payGroup === void 0 ? void 0 : payGroup.wait_amount) || 0) > 0 && 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 = _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 = 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 locales.getText("tocPay.text.submit");
		if (current === null || current === void 0 ? void 0 : current.surcharge) {
			var _state$payGroup18;
			return /* @__PURE__ */ React.createElement("span", { className: "" }, locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ React.createElement(PisellText.Amount, { value: ((_state$payGroup18 = state.payGroup) === null || _state$payGroup18 === void 0 ? void 0 : _state$payGroup18.wait_amount) || 0 }), " +", locales.getText("tocPay.text.surcharge"));
		} else {
			var _state$payGroup19;
			return /* @__PURE__ */ React.createElement("span", { className: "" }, locales.getText("tocPay.text.pay"), " ", /* @__PURE__ */ React.createElement(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 = _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} 用户取消支付`
		});
		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 }
		});
	};
	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]);
	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.createElement("div", { className: "custom-payment-list-title" }, /* @__PURE__ */ React.createElement("div", { className: "" }, locales.getText("tocPay.text.clickSubmitToComplete")), /* @__PURE__ */ React.createElement("div", { className: "custom-payment-list-title-desc" }, 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.createElement("div", { className: "custom-payment-list-title" }, locales.getText("tocPay.text.continuePayment"), " ", /* @__PURE__ */ React.createElement(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 locales.getText("tocPay.text.paymentMethod");
	};
	/**
	* @title: 钱包变化
	* @description:
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:50
	*/
	const onWalletPassChange = useMemoizedFn((walletPass) => {
		var _state$payGroup28, _state$payGroup29;
		const _waitAmount = new Decimal$1(String(((_state$payGroup28 = state.payGroup) === null || _state$payGroup28 === void 0 ? void 0 : _state$payGroup28.total_amount) || 0)).minus(new Decimal$1(String(walletPass.amount || 0))).toNumber();
		dispatch({
			type: "setPayGroup",
			payload: {
				walletPass: _objectSpread2(_objectSpread2({}, (_state$payGroup29 = state.payGroup) === null || _state$payGroup29 === void 0 ? void 0 : _state$payGroup29.walletPass), walletPass),
				wait_amount: _waitAmount
			}
		});
	});
	return /* @__PURE__ */ React.createElement(PisellContainer, {
		renderType,
		props: {
			forceRender: true,
			title: /* @__PURE__ */ React.createElement("div", null, 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.createElement(PisellModal.Information, {
		open: showInformation,
		title: locales.getText("tocPay.text.prompt"),
		describe: locales.getText("tocPay.text.confirmLeave"),
		okText: locales.getText("tocPay.text.continuePay"),
		cancelText: locales.getText("tocPay.text.leave"),
		closable: true,
		cancelButtonProps: { onClick: () => {
			onCancel();
		} },
		maskClosable: false,
		onCancel: () => {
			setShowInformation(false);
		},
		onOk: () => {
			setShowInformation(false);
		}
	}), /* @__PURE__ */ React.createElement(PisellModal.Information, {
		open: showPendingPayment,
		describe: locales.getText("tocPay.text.pendingPaymentDesc"),
		okText: locales.getText("tocPay.text.pendingPaymentOk"),
		cancelVisible: false,
		closable: false,
		maskClosable: false,
		onOk: () => {
			paymentLogger.sendLogs("此订单有一笔支付正在处理中");
			setShowPendingPayment(false);
			setOpen(false);
		}
	}), /* @__PURE__ */ React.createElement(PisellModal.Information, {
		open: showDetectIncognitoConfirm,
		describe: locales.getText("tocPay.text.detectIncognitoConfirm"),
		okText: locales.getText("tocPay.text.copyLink"),
		closable: true,
		maskClosable: false,
		onCancel: () => {
			setShowDetectIncognitoConfirm(false);
		},
		onOk: _asyncToGenerator(function* () {
			yield copyToClipboard(window.location.href);
			PisellToast({ content: locales.getText("tocPay.text.copySuccess") });
		})
	}), /* @__PURE__ */ React.createElement(PisellLoading, {
		loading,
		mask: true
	}, ((_state$order = state.order) === null || _state$order === void 0 ? void 0 : _state$order.countdown) && /* @__PURE__ */ React.createElement(PayCountdown, { time: (_state$order2 = state.order) === null || _state$order2 === void 0 ? void 0 : _state$order2.countdown }), /* @__PURE__ */ React.createElement(PayAmount, { 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.createElement(WalletPassBlock_default, {
		customer_id,
		payGroup: state.payGroup,
		onWalletPassChange,
		ref: walletPassBlockRef,
		walletSetting: state.walletSetting
	}), /* @__PURE__ */ React.createElement(PaymentList_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.createElement(PaymentMethods_default, {
		ref: paymentMethodsRef,
		payCallback
	}));
};
var toC_default = Provider(forwardRef(ToCPay));
//#endregion
export { toC_default as default };
