import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import { clientPayment, isMiniProgram, postMessageToMiniProgram } 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 { getWxPayUrl } 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/SuperPayWxPayH5/index.tsx
/**
* @title: 微信支付
* @description:
* @param {SuperPayWxPayH5Props} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:18
*/
const SuperPayWxPayH5 = (props, ref) => {
	var _context$appHelper, _state$payGroup;
	const { state, dispatch } = useContext(Context);
	const [open, setOpen] = useState(false);
	const { interaction, token } = ((_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:19
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			var _interaction$utils, _interaction$utils$is;
			const isMiniWx = isMiniProgram(interaction);
			paymentLogger.addLog({
				key: "SuperPayWxPayH5初始化开始",
				value: ""
			});
			const values = 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: "SuperPayWxPayH5App环境检测",
					value: "检测到App环境，调用客户端支付"
				});
				paymentLogger.sendLogs("SuperPayWxPayH5App环境支付");
				return clientPayment({
					order_id: payGroup.order_id,
					type: "wxPay",
					values,
					interaction
				});
			}
			setOpen(isMiniWx ? false : true);
			if (!values) {
				paymentLogger.addLog({
					key: "SuperPayWxPayH5获取支付参数失败",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("SuperPayWxPayH5获取支付参数失败");
				setOpen(false);
				return;
			}
			paymentLogger.addLog({
				key: "SuperPayWxPayH5获取支付参数成功",
				value: ""
			});
			try {
				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));
					postMessageToMiniProgram("superpay.wechat.h5", state);
					setOpen(false);
					return;
				}
				const data = yield getWxPayUrl(payGroup.order_id, values, state.history);
				if (data === null || data === void 0 ? void 0 : data.pay_url) {
					var _token$get2;
					paymentLogger.addLog({
						key: "SuperPayWxPayH5获取支付链接成功",
						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$get2 = token.get) === null || _token$get2 === void 0 ? void 0 : _token$get2.call(token));
					paymentLogger.addLog({
						key: "SuperPayWxPayH5准备跳转微信支付",
						value: ""
					});
					paymentLogger.sendLogs("SuperPayWxPayH5跳转成功");
					window.location.href = data === null || data === void 0 ? void 0 : data.pay_url;
				} else {
					paymentLogger.addLog({
						key: "SuperPayWxPayH5支付链接为空",
						value: {
							title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 微信支付链接为空`,
							data: { response_data: data }
						}
					});
					paymentLogger.sendLogs("SuperPayWxPayH5支付链接为空");
					setOpen(false);
				}
			} catch (err) {
				paymentLogger.addLog({
					key: "SuperPayWxPayH5处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} SuperPayWxPayH5处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							values
						}
					}
				});
				paymentLogger.sendLogs("SuperPayWxPayH5处理失败", 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.redirectingToWeChatPay") })));
};
var SuperPayWxPayH5_default = forwardRef(SuperPayWxPayH5);
//#endregion
export { SuperPayWxPayH5_default as default };
