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 { offlinePayment } from "./serve.js";
import { forwardRef, useContext, useImperativeHandle } from "react";
import "./index.less";
//#region src/components/pay/toC/PaymentMethods/OfflinePayment/index.tsx
/**
* @title: 线下支付
* @description:
* @param {OfflinePaymentProps} props
* @param {React} ref
* @return {*}
* @Author: zhiwei.Wang
* @Date: 2024-08-16 11:16
*/
const OfflinePayment = (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:17
	*/
	const init = function() {
		var _ref = _asyncToGenerator(function* ({ payGroup }) {
			try {
				var _item$params, _item$params2;
				paymentLogger.addLog({
					key: "OfflinePayment初始化开始",
					value: ""
				});
				const _item = payGroup === null || payGroup === void 0 ? void 0 : payGroup.otherPayment;
				const params = getPayGroupParamsFromStorage(payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id, {
					order_id: payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id,
					payment_method: _item === null || _item === void 0 || (_item$params = _item.params) === null || _item$params === void 0 ? void 0 : _item$params.payment_method
				});
				if (!params) {
					paymentLogger.addLog({
						key: "OfflinePayment获取支付参数失败",
						value: {
							title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 未找到存储的支付参数`,
							data: { payGroup }
						}
					});
					paymentLogger.sendLogs("OfflinePayment获取支付参数失败");
					return;
				}
				paymentLogger.addLog({
					key: "OfflinePayment获取支付参数成功",
					value: ""
				});
				paymentLogger.addLog({
					key: "OfflinePayment开始执行支付",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} 开始执行线下支付`,
						data: {
							params,
							paymentMethod: _item === null || _item === void 0 || (_item$params2 = _item.params) === null || _item$params2 === void 0 ? void 0 : _item$params2.payment_method
						}
					}
				});
				yield offlinePayment(params);
				paymentLogger.addLog({
					key: "OfflinePayment支付成功",
					value: ""
				});
				paymentLogger.sendLogs("OfflinePayment支付成功");
				state.payCallback({ key: "success" });
			} catch (err) {
				paymentLogger.addLog({
					key: "OfflinePayment处理错误",
					value: {
						title: `订单${payGroup === null || payGroup === void 0 ? void 0 : payGroup.order_id} OfflinePayment处理失败: ${(err === null || err === void 0 ? void 0 : err.message) || "未知错误"}`,
						data: { payGroup }
					}
				});
				paymentLogger.sendLogs("OfflinePayment处理失败", err);
			}
		});
		return function init(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	useImperativeHandle(ref, () => ({ init }));
	return null;
};
var OfflinePayment_default = forwardRef(OfflinePayment);
//#endregion
export { OfflinePayment_default as default };
