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 { walletPassPay } from "./WalletPass/serve.js";
import { formatWalletPassParams } from "../utils.js";
import { isCustomPayment } from "../PaymentList/status.js";
import { clearStashPayGroup, removePayGroupFromStorage, setPayNumber, syncPayGroupToStorage } from "../payGroup.js";
import { Context } from "../model.js";
import MWCreditCard_default from "./MWCreditCard/index.js";
import SuperPayAliPayH5_default from "./SuperPayAliPayH5/index.js";
import StripePay_default from "./StripePay/index.js";
import OfflinePayment_default from "./OfflinePayment/index.js";
import BankDeposit_default from "./BankDeposit/index.js";
import SuperPayWxPayH5_default from "./SuperPayWxPayH5/index.js";
import CustomPayment_default from "./CustomPayment/index.js";
import CashManual_default from "./CashManual/index.js";
import GooglePay_default from "./GooglePay/index.js";
import ApplePay_default from "./ApplePay/index.js";
import GlobePayAliPayH5_default from "./GlobePayAliPayH5/index.js";
import GlobePayPaypalH5_default from "./GlobePayPaypalH5/index.js";
import GlobePayWxH5_default from "./GlobePayWxH5/index.js";
import GlobePayQrcode_default from "./GlobePayQrcode/index.js";
import MiniProgramWaitingPaymentModal_default from "./components/MiniProgramWaitingPaymentModal/index.js";
import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef } from "react";
import { isJson, sendWarningLog } from "@pisell/utils";
//#region src/components/pay/toC/PaymentMethods/index.tsx
const isOfflinePayment = (type) => {
	if (isCustomPayment(type)) return true;
	return [
		"pisell.bank_deposit",
		"pisell.offline_payment",
		"pisell.other.cashmanual",
		"pisell.custom_bank_transfer"
	].includes(type);
};
const PaymentMethods = (props, ref) => {
	var _context$appHelper, _context$appHelper2, _context$appHelper2$g, _dvaStore$core, _interaction$utils, _interaction$utils$is;
	const { state } = useContext(Context);
	const MWCreditCardRef = useRef(null);
	const SuperPayAliPayH5Ref = useRef(null);
	const StripePayRef = useRef(null);
	const OfflinePaymentRef = useRef(null);
	const BankDepositRef = useRef(null);
	const SuperPayWxPayH5Ref = useRef(null);
	const CustomPaymentRef = useRef(null);
	const ApplePayRef = useRef(null);
	const GooglePayRef = useRef(null);
	const CashManualRef = useRef(null);
	const GlobePayAliPayH5Ref = useRef(null);
	const GlobePayPaypalH5Ref = useRef(null);
	const GlobePayWxH5Ref = useRef(null);
	const GlobePayQrcodeRef = useRef(null);
	const MiniProgramWaitingPaymentModalRef = useRef(null);
	const context = useEngineContext();
	const { interaction, token } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const dvaStore = (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 || (_context$appHelper2$g = _context$appHelper2.getStore) === null || _context$appHelper2$g === void 0 || (_context$appHelper2$g = _context$appHelper2$g.call(_context$appHelper2)) === null || _context$appHelper2$g === void 0 ? void 0 : _context$appHelper2$g.getState();
	const shop = (dvaStore === null || dvaStore === void 0 || (_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 || (_dvaStore$core = _dvaStore$core.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.shop) || {};
	const shopName = (shop === null || shop === void 0 ? void 0 : shop.name) || "";
	const shopPhone = (shop === null || shop === void 0 ? void 0 : shop.phone) || "";
	const shopEmail = (shop === null || shop === void 0 ? void 0 : shop.contact_email) || "";
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ type, payGroup }) {
			var _payGroup$walletPass;
			const waitAmount = (payGroup === null || payGroup === void 0 ? void 0 : payGroup.wait_amount) || 0;
			const cards = payGroup === null || payGroup === void 0 || (_payGroup$walletPass = payGroup.walletPass) === null || _payGroup$walletPass === void 0 || (_payGroup$walletPass = _payGroup$walletPass.cards) === null || _payGroup$walletPass === void 0 ? void 0 : _payGroup$walletPass.length;
			sendWarningLog({
				title: "支付初始化",
				content: [
					{
						key: "type",
						value: type
					},
					{
						key: "payGroup",
						value: JSON.stringify(payGroup)
					},
					{
						key: "waitAmount",
						value: waitAmount
					},
					{
						key: "cards",
						value: cards
					}
				]
			});
			if (cards && (waitAmount <= 0 || isOfflinePayment(type))) try {
				yield walletPassPay(formatWalletPassParams(payGroup));
				yield clearStashPayGroup(payGroup.order_id);
				syncPayGroupToStorage(_objectSpread2(_objectSpread2({}, payGroup), {}, { walletPass: _objectSpread2(_objectSpread2({}, payGroup.walletPass), {}, {
					cards: [],
					paid: true
				}) }));
			} catch (err) {
				sendWarningLog({
					title: "钱包支付失败 catch",
					content: [
						{
							key: "错误信息",
							value: (err === null || err === void 0 ? void 0 : err.message) || "未知错误"
						},
						{
							key: "错误堆栈",
							value: (err === null || err === void 0 ? void 0 : err.stack) || "无堆栈信息"
						},
						{
							key: "错误类型",
							value: (err === null || err === void 0 ? void 0 : err.name) || "未知错误类型"
						}
					]
				});
				return props.payCallback({
					key: "fail",
					data: {
						err,
						payGroup,
						fn: "init"
					}
				});
			}
			if (waitAmount <= 0) {
				try {
					removePayGroupFromStorage(payGroup.order_id);
					yield clearStashPayGroup(payGroup.order_id);
					props.payCallback({ key: "success" });
				} catch (err) {
					sendWarningLog({
						title: "跳转成功页面 catch",
						content: [
							{
								key: "错误信息",
								value: (err === null || err === void 0 ? void 0 : err.message) || "未知错误"
							},
							{
								key: "错误堆栈",
								value: (err === null || err === void 0 ? void 0 : err.stack) || "无堆栈信息"
							},
							{
								key: "错误类型",
								value: (err === null || err === void 0 ? void 0 : err.name) || "未知错误类型"
							}
						]
					});
				}
				return;
			}
			yield onPayByType({
				type,
				payGroup
			});
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	console.log("paymentResult isAppEnv", 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), interaction, context);
	useEffect(() => {
		var _interaction$utils2, _interaction$utils2$i, _interaction$utils3, _interaction$utils3$i;
		console.log("paymentResult isAppEnv", interaction === null || interaction === void 0 || (_interaction$utils2 = interaction.utils) === null || _interaction$utils2 === void 0 || (_interaction$utils2$i = _interaction$utils2.isAppEnv) === null || _interaction$utils2$i === void 0 ? void 0 : _interaction$utils2$i.call(_interaction$utils2));
		if (interaction === null || interaction === void 0 || (_interaction$utils3 = interaction.utils) === null || _interaction$utils3 === void 0 || (_interaction$utils3$i = _interaction$utils3.isAppEnv) === null || _interaction$utils3$i === void 0 ? void 0 : _interaction$utils3$i.call(_interaction$utils3)) interaction.utils.mountFunction("global", "paymentResult", (strResult) => {
			console.log("paymentResult", strResult);
			if (!isJson(strResult)) return;
			const { type, pay_number, order_id } = JSON.parse(strResult);
			_asyncToGenerator(function* () {
				if (type && order_id && pay_number) {
					var _token$get;
					yield setPayNumber(order_id, pay_number, token === null || token === void 0 || (_token$get = token.get) === null || _token$get === void 0 ? void 0 : _token$get.call(token));
				}
				props.payCallback({ key: "success" });
			})();
		});
	}, []);
	const onPayByType = function() {
		var _ref2 = _asyncToGenerator(function* ({ type, payGroup }) {
			console.log(type, payGroup);
			if (type === "mw.apple") {
				var _ApplePayRef$current;
				yield (_ApplePayRef$current = ApplePayRef.current) === null || _ApplePayRef$current === void 0 ? void 0 : _ApplePayRef$current.init({ payGroup });
				return;
			}
			if (type === "mw.google") {
				var _GooglePayRef$current;
				yield (_GooglePayRef$current = GooglePayRef.current) === null || _GooglePayRef$current === void 0 ? void 0 : _GooglePayRef$current.init({ payGroup });
				return;
			}
			if (type === "mw.credit_card") {
				var _MWCreditCardRef$curr;
				(_MWCreditCardRef$curr = MWCreditCardRef.current) === null || _MWCreditCardRef$curr === void 0 || _MWCreditCardRef$curr.init({ payGroup });
				return;
			}
			if (type === "superpay.alipay.h5") {
				var _SuperPayAliPayH5Ref$;
				yield (_SuperPayAliPayH5Ref$ = SuperPayAliPayH5Ref.current) === null || _SuperPayAliPayH5Ref$ === void 0 ? void 0 : _SuperPayAliPayH5Ref$.init({ payGroup });
				return;
			}
			if (type === "superpay.wechat.h5") {
				var _SuperPayWxPayH5Ref$c;
				yield (_SuperPayWxPayH5Ref$c = SuperPayWxPayH5Ref.current) === null || _SuperPayWxPayH5Ref$c === void 0 ? void 0 : _SuperPayWxPayH5Ref$c.init({ payGroup });
				return;
			}
			if (type === "stripe") {
				var _StripePayRef$current;
				(_StripePayRef$current = StripePayRef.current) === null || _StripePayRef$current === void 0 || _StripePayRef$current.init({ payGroup });
				return;
			}
			if (type === "pisell.offline_payment") {
				var _OfflinePaymentRef$cu;
				yield (_OfflinePaymentRef$cu = OfflinePaymentRef.current) === null || _OfflinePaymentRef$cu === void 0 ? void 0 : _OfflinePaymentRef$cu.init({ payGroup });
				return;
			}
			if (type === "pisell.bank_deposit") {
				var _BankDepositRef$curre;
				yield (_BankDepositRef$curre = BankDepositRef.current) === null || _BankDepositRef$curre === void 0 ? void 0 : _BankDepositRef$curre.init({ payGroup });
				return;
			}
			if (type === "pisell.other.cashmanual") {
				var _CashManualRef$curren;
				yield (_CashManualRef$curren = CashManualRef.current) === null || _CashManualRef$curren === void 0 ? void 0 : _CashManualRef$curren.init({ payGroup });
				return;
			}
			if (type === "globepay.alipay.h5") {
				var _GlobePayAliPayH5Ref$;
				yield (_GlobePayAliPayH5Ref$ = GlobePayAliPayH5Ref.current) === null || _GlobePayAliPayH5Ref$ === void 0 ? void 0 : _GlobePayAliPayH5Ref$.init({ payGroup });
				return;
			}
			if (type === "globepay.paypal.h5") {
				var _GlobePayPaypalH5Ref$;
				yield (_GlobePayPaypalH5Ref$ = GlobePayPaypalH5Ref.current) === null || _GlobePayPaypalH5Ref$ === void 0 ? void 0 : _GlobePayPaypalH5Ref$.init({ payGroup });
				return;
			}
			if (type === "globepay.wechat.jsapi") {
				var _GlobePayWxH5Ref$curr;
				yield (_GlobePayWxH5Ref$curr = GlobePayWxH5Ref.current) === null || _GlobePayWxH5Ref$curr === void 0 ? void 0 : _GlobePayWxH5Ref$curr.init({ payGroup });
				return;
			}
			if (type === "globepay.alipay.qrcode" || type === "globepay.wechat.qrcode") {
				var _GlobePayQrcodeRef$cu;
				yield (_GlobePayQrcodeRef$cu = GlobePayQrcodeRef.current) === null || _GlobePayQrcodeRef$cu === void 0 ? void 0 : _GlobePayQrcodeRef$cu.init({ payGroup });
				return;
			}
			if (isCustomPayment(type)) {
				var _CustomPaymentRef$cur;
				yield (_CustomPaymentRef$cur = CustomPaymentRef.current) === null || _CustomPaymentRef$cur === void 0 ? void 0 : _CustomPaymentRef$cur.init({ payGroup });
				return;
			}
		});
		return function onPayByType(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ onPay: init }));
	useEffect(() => {
		var _MiniProgramWaitingPa, _state$payGroup;
		const { open, paymentUrl } = state.miniProgramPayment || {};
		(_MiniProgramWaitingPa = MiniProgramWaitingPaymentModalRef.current) === null || _MiniProgramWaitingPa === void 0 || _MiniProgramWaitingPa.init({
			open: open || false,
			paymentUrl: paymentUrl || "",
			orderId: (state === null || state === void 0 || (_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || ""
		});
	}, [state === null || state === void 0 ? void 0 : state.miniProgramPayment]);
	return /* @__PURE__ */ React.createElement("div", { className: "" }, /* @__PURE__ */ React.createElement(MWCreditCard_default, {
		ref: MWCreditCardRef,
		shopName,
		shopPhone,
		shopEmail
	}), /* @__PURE__ */ React.createElement(SuperPayAliPayH5_default, { ref: SuperPayAliPayH5Ref }), /* @__PURE__ */ React.createElement(StripePay_default, { ref: StripePayRef }), /* @__PURE__ */ React.createElement(OfflinePayment_default, { ref: OfflinePaymentRef }), /* @__PURE__ */ React.createElement(BankDeposit_default, { ref: BankDepositRef }), /* @__PURE__ */ React.createElement(SuperPayWxPayH5_default, { ref: SuperPayWxPayH5Ref }), /* @__PURE__ */ React.createElement(CustomPayment_default, { ref: CustomPaymentRef }), /* @__PURE__ */ React.createElement(ApplePay_default, { ref: ApplePayRef }), /* @__PURE__ */ React.createElement(GooglePay_default, { ref: GooglePayRef }), /* @__PURE__ */ React.createElement(CashManual_default, { ref: CashManualRef }), /* @__PURE__ */ React.createElement(GlobePayAliPayH5_default, { ref: GlobePayAliPayH5Ref }), /* @__PURE__ */ React.createElement(GlobePayWxH5_default, { ref: GlobePayWxH5Ref }), /* @__PURE__ */ React.createElement(GlobePayPaypalH5_default, { ref: GlobePayPaypalH5Ref }), /* @__PURE__ */ React.createElement(GlobePayQrcode_default, { ref: GlobePayQrcodeRef }), /* @__PURE__ */ React.createElement(MiniProgramWaitingPaymentModal_default, {
		onAnotherPayment: () => {
			var _MWCreditCardRef$curr2, _MWCreditCardRef$curr3;
			return (_MWCreditCardRef$curr2 = MWCreditCardRef.current) === null || _MWCreditCardRef$curr2 === void 0 || (_MWCreditCardRef$curr3 = _MWCreditCardRef$curr2.closeAll) === null || _MWCreditCardRef$curr3 === void 0 ? void 0 : _MWCreditCardRef$curr3.call(_MWCreditCardRef$curr2);
		},
		ref: MiniProgramWaitingPaymentModalRef
	}));
};
var PaymentMethods_default = forwardRef(PaymentMethods);
//#endregion
export { PaymentMethods_default as default };
