const require_runtime = require("../../../../../_virtual/_rolldown/runtime.js");
const require_asyncToGenerator = require("../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../../../hooks/useEngineContext.js");
const require_payGroup = require("../../payGroup.js");
const require_model = require("../../model.js");
const require_index = require("../MWCreditCard/TDSPay/ThreedsLoading/index.js");
const require_usePaymentLogger = require("../../../../../hooks/usePaymentLogger.js");
const require_serve = require("./serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./index.less");
//#region src/components/pay/toC/PaymentMethods/GlobePayAliPayH5/index.tsx
/**
* @title: 支付宝支付
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:18
*/
const GlobePayAliPayH5 = (props, ref) => {
	var _context$appHelper, _state$payGroup;
	const { onChange } = props;
	const [open, setOpen] = (0, react.useState)(false);
	const { state } = (0, react.useContext)(require_model.Context);
	const [showSuccessBackButton, setShowSuccessBackButton] = (0, react.useState)(false);
	const { token, interaction } = ((_context$appHelper = require_useEngineContext.default().appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils) || {};
	const paymentLogger = require_usePaymentLogger.default({
		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 = require_asyncToGenerator._asyncToGenerator(function* ({ payGroup }) {
			paymentLogger.addLog({
				key: "GlobePayAliPayH5初始化开始",
				value: ""
			});
			setOpen(true);
			const values = require_payGroup.getPayGroupParamsFromStorage(payGroup.order_id);
			if (!values) {
				paymentLogger.addLog({
					key: "GlobePayAliPayH5获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("GlobePayAliPayH5获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "GlobePayAliPayH5获取支付参数成功",
				value: ""
			});
			try {
				paymentLogger.addLog({
					key: "GlobePayAliPayH5开始获取支付链接",
					value: ""
				});
				const data = yield require_serve.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: "GlobePayAliPayH5获取支付链接成功",
						value: ""
					});
					yield require_payGroup.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: "GlobePayAliPayH5准备跳转支付宝",
						value: ""
					});
					setShowSuccessBackButton(true);
					paymentLogger.sendLogs("GlobePayAliPayH5跳转成功");
					window.location.href = url;
				} else {
					paymentLogger.addLog({
						key: "GlobePayAliPayH5未获取到支付链接",
						value: {
							title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未获取到有效的支付链接`,
							data: {
								payGroup,
								response: data
							}
						}
					});
					paymentLogger.sendLogs("GlobePayAliPayH5未获取到支付链接");
					setOpen(false);
				}
			} catch (err) {
				paymentLogger.addLog({
					key: "GlobePayAliPayH5处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} GlobePayAliPayH5处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("GlobePayAliPayH5处理失败", err);
				setOpen(false);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useImperativeHandle)(ref, () => ({ init }));
	const handleSuccessBack = () => {
		var _state$payCallback;
		paymentLogger.addLog({
			key: "GlobePayAliPayH5用户确认支付成功",
			value: ""
		});
		paymentLogger.sendLogs("GlobePayAliPayH5支付成功");
		(_state$payCallback = state.payCallback) === null || _state$payCallback === void 0 || _state$payCallback.call(state, { key: "success" });
	};
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		platform: "h5",
		open,
		footer: false,
		header: false,
		closable: false,
		bodyStyle: { padding: 0 }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "" }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		title: _pisell_utils.locales.getText("tocPay.text.redirectingToAlipay"),
		onSuccessBack: handleSuccessBack,
		showSuccessBackButton
	})));
};
var GlobePayAliPayH5_default = (0, react.forwardRef)(GlobePayAliPayH5);
//#endregion
exports.default = GlobePayAliPayH5_default;
