import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../../hooks/useEngineContext.js";
import { isMiniProgram, openMiniProgramPaymentModal } from "../../utils.js";
import { Context } from "../../model.js";
import usePaymentLogger from "../../../../../hooks/usePaymentLogger.js";
import { forwardRef, useContext, useImperativeHandle } from "react";
import "./index.less";
//#region src/components/pay/toC/PaymentMethods/StripePay/index.tsx
/**
* @title: StripePay
* @description:
* @param {StripePayProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:17
*/
const StripePay = (props, ref) => {
	var _context$appHelper, _context$appHelper2, _state$payGroup;
	const { state, dispatch } = useContext(Context);
	const context = useEngineContext();
	const { config } = ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.constants) || {};
	const { interaction } = ((_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.utils) || {};
	const paymentLogger = usePaymentLogger({
		orderId: (state === null || state === void 0 || (_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:17
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			paymentLogger.addLog({
				key: "StripePay初始化开始",
				value: ""
			});
			try {
				var _context$appHelper3;
				let _item = payGroup === null || payGroup === void 0 ? void 0 : payGroup.otherPayment;
				const platform = ((_context$appHelper3 = context.appHelper) === null || _context$appHelper3 === void 0 || (_context$appHelper3 = _context$appHelper3.constants) === null || _context$appHelper3 === void 0 ? void 0 : _context$appHelper3.platform) || "h5";
				paymentLogger.addLog({
					key: "StripePay平台检测",
					value: {
						platform,
						paymentItem: _item
					}
				});
				if (platform === "pc") {
					paymentLogger.addLog({
						key: "StripePay PC端回调准备",
						value: {
							service: _item.service,
							orderId: payGroup.order_id,
							paymentMethod: _item.title
						}
					});
					state.payCallback({
						key: _item.service,
						data: {
							order_id: payGroup.order_id,
							payment_method: _item.title
						}
					});
					paymentLogger.sendLogs("StripePay PC端回调成功");
					return;
				}
				const redirectUrl = `/pay/stripe?order_id=${payGroup.order_id}&payment_method=${encodeURIComponent(_item.title)}`;
				paymentLogger.addLog({
					key: "StripePay H5端跳转准备",
					value: { redirectUrl }
				});
				if (isMiniProgram(interaction)) {
					openMiniProgramPaymentModal(`${config === null || config === void 0 ? void 0 : config.domain}${redirectUrl}`, dispatch);
					return;
				}
				window.location.href = redirectUrl;
				paymentLogger.sendLogs("StripePay H5端跳转成功");
			} catch (err) {
				paymentLogger.addLog({
					key: "StripePay处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} StripePay处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("StripePay处理失败", err);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ init }));
	return null;
};
var StripePay_default = forwardRef(StripePay);
//#endregion
export { StripePay_default as default };
