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_utils = require("../../utils.js");
const require_payGroup = require("../../payGroup.js");
const require_model = require("../../model.js");
const require_index = require("../MWCreditCard/TDSPay/ThreedsLoading/index.js");
const require_serve = require("./serve.js");
const require_usePaymentLogger = require("../../../../../hooks/usePaymentLogger.js");
const require_index$1 = require("../hooks/index.js");
const require_index$2 = require("../components/PaymentReturnContent/index.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/SuperPayAliPayH5/index.tsx
/**
* @title: 支付宝支付
* @description:
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:18
*/
const SuperPayAliPayH5 = (props, ref) => {
	var _context$appHelper, _state$payGroup, _state$payGroup2, _state$payGroup3;
	const { state, dispatch } = (0, react.useContext)(require_model.Context);
	const { onChange } = props;
	const [open, setOpen] = (0, react.useState)(false);
	const [showSuccessBackButton, setShowSuccessBackButton] = (0, react.useState)(false);
	const [statePayNumber, setStatePayNumber] = (0, react.useState)("");
	const { interaction, token } = (_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"
	});
	const { isReturned, handleRedirect, resetState } = require_index$1.usePaymentRedirect({
		orderId: (_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.order_id,
		paymentType: "aliPay"
	});
	/**
	* @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 }) {
			var _interaction$utils, _interaction$utils$is;
			const isMiniWx = require_utils.isMiniProgram(interaction);
			paymentLogger.addLog({
				key: "SuperPayAliPayH5初始化开始",
				value: ""
			});
			const values = require_payGroup.getPayGroupParamsFromStorage(payGroup.order_id);
			if (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)) {
				paymentLogger.addLog({
					key: "SuperPayAliPayH5App环境检测",
					value: "检测到App环境，调用客户端支付"
				});
				paymentLogger.sendLogs("SuperPayAliPayH5App环境支付");
				return require_utils.clientPayment({
					order_id: payGroup.order_id,
					type: "aliPay",
					values,
					interaction
				});
			}
			setOpen(isMiniWx ? false : true);
			if (!values) {
				paymentLogger.addLog({
					key: "SuperPayAliPayH5获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("SuperPayAliPayH5获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "SuperPayAliPayH5获取支付参数成功",
				value: ""
			});
			try {
				const data = yield require_serve.getAliPayUrl(payGroup.order_id, values, state.history);
				if (data === null || data === void 0 ? void 0 : data.pay_url) {
					var _token$get;
					paymentLogger.addLog({
						key: "SuperPayAliPayH5获取支付链接成功",
						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));
					setStatePayNumber(data === null || data === void 0 ? void 0 : data.pay_number);
					setShowSuccessBackButton(true);
					paymentLogger.addLog({
						key: "SuperPayAliPayH5准备跳转支付宝",
						value: ""
					});
					paymentLogger.sendLogs("SuperPayAliPayH5跳转成功");
					if (isMiniWx) {
						require_utils.openMiniProgramPaymentModal(data === null || data === void 0 ? void 0 : data.pay_url, dispatch);
						setOpen(false);
						return;
					}
					yield handleRedirect(data === null || data === void 0 ? void 0 : data.pay_url);
				} else {
					paymentLogger.addLog({
						key: "SuperPayAliPayH5支付链接为空",
						value: {
							title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 支付宝支付链接为空`,
							data: { response_data: data }
						}
					});
					paymentLogger.sendLogs("SuperPayAliPayH5支付链接为空");
					setOpen(false);
				}
			} catch (err) {
				paymentLogger.addLog({
					key: "SuperPayAliPayH5处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} SuperPayAliPayH5处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("SuperPayAliPayH5处理失败", err);
				setOpen(false);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	(0, react.useImperativeHandle)(ref, () => ({ init }));
	const handleSuccessBack = () => {
		var _state$payCallback;
		(_state$payCallback = state.payCallback) === null || _state$payCallback === void 0 || _state$payCallback.call(state, { key: "success" });
	};
	const handleReturn = () => {
		setOpen(false);
		resetState();
	};
	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: "" }, !isReturned && /* @__PURE__ */ react.default.createElement(require_index.default, {
		title: _pisell_utils.locales.getText("tocPay.text.redirectingToAlipay"),
		onSuccessBack: handleSuccessBack,
		showSuccessBackButton
	}), isReturned && /* @__PURE__ */ react.default.createElement(require_index$2.default, {
		pay_number: statePayNumber,
		handleReturn,
		state,
		orderId: (_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.order_id,
		unique_payment_number: state.front_order_uuid
	})));
};
var SuperPayAliPayH5_default = (0, react.forwardRef)(SuperPayAliPayH5);
//#endregion
exports.default = SuperPayAliPayH5_default;
