import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import { formatFranchiseeRoute, isMiniProgram, openMiniProgramPaymentModal } from "../../utils.js";
import { getPayGroupParamsFromStorage, setPayNumber } from "../../payGroup.js";
import { Context } from "../../model.js";
import ThreedsLoading from "../MWCreditCard/TDSPay/ThreedsLoading/index.js";
import usePaymentLogger from "../../../../../hooks/usePaymentLogger.js";
import { usePaymentRedirect } from "../hooks/index.js";
import PaymentReturnContent from "../components/PaymentReturnContent/index.js";
import React, { forwardRef, useContext, useImperativeHandle, useState } from "react";
import { locales } from "@pisell/utils";
import { PisellModal } from "@pisell/materials";
import "./index.less";
//#region src/components/pay/toC/PaymentMethods/ApplePay/index.tsx
/**
* @title: Apple 支付
* @description:
* @param {ApplePayProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 10:58
*/
const ApplePay = (props, ref) => {
	var _context$appHelper, _context$appHelper2, _state$payGroup, _state$payGroup2, _state$payGroup4;
	const { state, dispatch } = useContext(Context);
	const [open, setOpen] = useState(false);
	const context = useEngineContext();
	const { config } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.constants) || {};
	const { interaction, token } = ((_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.utils) || {};
	const paymentLogger = usePaymentLogger({
		orderId: ((_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || "unknown",
		step: "2"
	});
	const { isReturned, handleRedirect, resetState } = usePaymentRedirect({
		orderId: (_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.order_id,
		paymentType: "applePay"
	});
	/**
	* @title: 初始化
	* @description:
	* @param {any} param1
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 10:58
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			var _utils$getStore;
			const isMiniWx = isMiniProgram(interaction);
			paymentLogger.addLog({
				key: "ApplePay初始化开始",
				value: ""
			});
			setOpen(isMiniWx ? false : true);
			const utils = context.appHelper.utils || {};
			const dvaStore = utils === null || utils === void 0 || (_utils$getStore = utils.getStore) === null || _utils$getStore === void 0 || (_utils$getStore = _utils$getStore.call(utils)) === null || _utils$getStore === void 0 ? void 0 : _utils$getStore.getState();
			const values = getPayGroupParamsFromStorage(payGroup.order_id);
			if (!values) {
				paymentLogger.addLog({
					key: "ApplePay获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("ApplePay获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "ApplePay获取支付参数成功",
				value: ""
			});
			try {
				var _dvaStore$core, _dvaStore$core2, _state$payGroup3;
				const locale = ((_dvaStore$core = dvaStore.core) === null || _dvaStore$core === void 0 ? void 0 : _dvaStore$core.translateLocale) || "en";
				const shopId = (_dvaStore$core2 = dvaStore.core) === null || _dvaStore$core2 === void 0 || (_dvaStore$core2 = _dvaStore$core2.core) === null || _dvaStore$core2 === void 0 || (_dvaStore$core2 = _dvaStore$core2.shop) === null || _dvaStore$core2 === void 0 ? void 0 : _dvaStore$core2.id;
				const currentURL = window.location.href;
				const linkElement = document.createElement("a");
				linkElement.href = currentURL;
				const shop_domain = linkElement.hostname;
				const _href = `${config.pay}${formatFranchiseeRoute(`/apple-pay?order_id=${(_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.order_id}&shop_id=${shopId}&locale=${locale}&shop_domain=${shop_domain}&total_amount=${values.amount}&payment_group_id=${values.payment_group_id}&front_order_uuid=${state.front_order_uuid}&is_replace_router=1`, state.history)}`;
				paymentLogger.addLog({
					key: "ApplePay准备跳转",
					value: ""
				});
				paymentLogger.sendLogs("ApplePay跳转成功");
				if (isMiniWx) {
					var _token$get;
					yield setPayNumber(payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id, "", token === null || token === void 0 || (_token$get = token.get) === null || _token$get === void 0 ? void 0 : _token$get.call(token));
					openMiniProgramPaymentModal(_href, dispatch);
					setOpen(false);
					return;
				}
				yield handleRedirect(_href);
			} catch (err) {
				paymentLogger.addLog({
					key: "ApplePay处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} ApplePay处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("ApplePay处理失败", err);
				setOpen(false);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ init }));
	const handleReturn = () => {
		setOpen(false);
		resetState();
	};
	return /* @__PURE__ */ React.createElement(PisellModal, {
		platform: "h5",
		open,
		footer: false,
		header: false,
		closable: false,
		bodyStyle: { padding: 0 }
	}, /* @__PURE__ */ React.createElement("div", { className: "" }, !isReturned && /* @__PURE__ */ React.createElement(ThreedsLoading, { title: locales.getText("tocPay.text.connectingApplePay") }), isReturned && /* @__PURE__ */ React.createElement(PaymentReturnContent, {
		handleReturn,
		state,
		orderId: (_state$payGroup4 = state.payGroup) === null || _state$payGroup4 === void 0 ? void 0 : _state$payGroup4.order_id,
		unique_payment_number: state === null || state === void 0 ? void 0 : state.front_order_uuid
	})));
};
var ApplePay_default = forwardRef(ApplePay);
//#endregion
export { ApplePay_default as default };
