import { _objectSpread2 } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { getPayGroupParamsFromStorage } from "../../payGroup.js";
import { Context } from "../../model.js";
import usePaymentLogger from "../../../../../hooks/usePaymentLogger.js";
import { customPaymentApi } from "./serve.js";
import { forwardRef, useContext, useImperativeHandle } from "react";
import "./index.less";
//#region src/components/pay/toC/PaymentMethods/CustomPayment/index.tsx
/**
* @title: 自定义支付
* @description:
* @param {CustomPaymentProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:01
*/
const CustomPayment = (props, ref) => {
	var _state$payGroup;
	const { state } = useContext(Context);
	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:01
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			var _state$payGroup2, _state$payGroup3;
			paymentLogger.addLog({
				key: "CustomPayment初始化开始",
				value: ""
			});
			let current = ((_state$payGroup2 = state.payGroup) === null || _state$payGroup2 === void 0 ? void 0 : _state$payGroup2.otherPayment) || {};
			const orderId = (payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id) || ((_state$payGroup3 = state.payGroup) === null || _state$payGroup3 === void 0 ? void 0 : _state$payGroup3.order_id);
			const values = getPayGroupParamsFromStorage(orderId);
			if (!values) {
				paymentLogger.addLog({
					key: "CustomPayment获取支付参数失败",
					value: {
						title: `订单${orderId} 未找到存储的支付参数`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("CustomPayment获取支付参数失败");
				return;
			}
			paymentLogger.addLog({
				key: "CustomPayment获取支付参数成功",
				value: ""
			});
			try {
				paymentLogger.addLog({
					key: "CustomPayment开始支付API调用",
					value: ""
				});
				yield customPaymentApi(orderId, _objectSpread2({ params: current.params }, values));
				paymentLogger.addLog({
					key: "CustomPayment支付API调用成功",
					value: ""
				});
				paymentLogger.sendLogs("CustomPayment支付成功");
				state.payCallback({ key: "success" });
			} catch (err) {
				paymentLogger.addLog({
					key: "CustomPayment支付错误",
					value: {
						title: `订单${orderId} CustomPayment支付失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: {
							payGroup,
							current,
							values
						}
					}
				});
				paymentLogger.sendLogs("CustomPayment支付失败", err);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ init }));
	return null;
};
var CustomPayment_default = forwardRef(CustomPayment);
//#endregion
export { CustomPayment_default as default };
