import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.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 { getAliPayUrl } from "./serve.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/GlobePayPaypalH5/index.tsx
/**
* @title: Paypal支付
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:18
*/
const GlobePayPaypalH5 = (props, ref) => {
	var _context$appHelper, _state$payGroup;
	const { state } = useContext(Context);
	const { onChange } = props;
	const [open, setOpen] = useState(false);
	const { token, interaction } = ((_context$appHelper = useEngineContext().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const paymentLogger = usePaymentLogger({
		orderId: ((_state$payGroup = state.payGroup) === null || _state$payGroup === void 0 ? void 0 : _state$payGroup.order_id) || "unknown",
		step: "2"
	});
	/**
	* @title: 初始化
	* @description:
	* @param {any} param1
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-08-16 11:18
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			paymentLogger.addLog({
				key: "GlobePayPaypalH5初始化开始",
				value: ""
			});
			setOpen(true);
			const values = getPayGroupParamsFromStorage(payGroup.order_id);
			if (!values) {
				paymentLogger.addLog({
					key: "GlobePayPaypalH5获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("GlobePayPaypalH5获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "GlobePayPaypalH5获取支付参数成功",
				value: ""
			});
			try {
				paymentLogger.addLog({
					key: "GlobePayPaypalH5开始获取支付链接",
					value: ""
				});
				const data = yield getAliPayUrl(payGroup.order_id, values, state.history);
				const url = data.url || (data === null || data === void 0 ? void 0 : data.pay_url);
				if (url) {
					var _token$get;
					paymentLogger.addLog({
						key: "GlobePayPaypalH5获取支付链接成功",
						value: ""
					});
					yield setPayNumber(payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id, data === null || data === void 0 ? void 0 : data.pay_number, token === null || token === void 0 || (_token$get = token.get) === null || _token$get === void 0 ? void 0 : _token$get.call(token));
					paymentLogger.addLog({
						key: "GlobePayPaypalH5准备跳转Paypal",
						value: ""
					});
					paymentLogger.sendLogs("GlobePayPaypalH5跳转成功");
					window.location.href = url;
				} else {
					paymentLogger.addLog({
						key: "GlobePayPaypalH5未获取到支付链接",
						value: {
							title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未获取到有效的支付链接`,
							data: {
								payGroup,
								response: data
							}
						}
					});
					paymentLogger.sendLogs("GlobePayPaypalH5未获取到支付链接");
					setOpen(false);
				}
			} catch (err) {
				paymentLogger.addLog({
					key: "GlobePayPaypalH5处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} GlobePayPaypalH5处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("GlobePayPaypalH5处理失败", err);
				setOpen(false);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ init }));
	return /* @__PURE__ */ React.createElement(PisellModal, {
		platform: "h5",
		open,
		footer: false,
		header: false,
		closable: false,
		bodyStyle: { padding: 0 }
	}, /* @__PURE__ */ React.createElement("div", { className: "" }, /* @__PURE__ */ React.createElement(ThreedsLoading, { title: locales.getText("tocPay.text.redirectingToPaypal") })));
};
var GlobePayPaypalH5_default = forwardRef(GlobePayPaypalH5);
//#endregion
export { GlobePayPaypalH5_default as default };
