import { _objectSpread2 } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { ModeEnum } from "../../const.js";
import { getUuid } from "../../helper.js";
import { updateState, useContextData, useDispatch } from "../../store/index.js";
import { useStoreRef } from "../../hooks.js";
import PaymentMethod from "../utils/payment.js";
import { useEffect, useRef } from "react";
//#region src/components/eftposPay/aggregatePayment/hooks/usePayment.ts
const usePayment = (options) => {
	/** 初始化参数 */
	const optionsRef = useRef(options);
	const { custom } = useContextData();
	const { modeRef, payRef, orderIdRef, eftposRef, deviceRef, dataRef, amountRef } = useStoreRef();
	const paymentMethodRef = useRef(null);
	const dispatch = useDispatch();
	const uniquePaymentNumberRef = useRef(null);
	const isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
	const initPayment = () => {
		if (!paymentMethodRef.current) {
			var _optionsRef$current, _deviceRef$current;
			if (!((_optionsRef$current = optionsRef.current) === null || _optionsRef$current === void 0 ? void 0 : _optionsRef$current.paymentNum)) {
				const paymentNum = getUuid();
				dispatch(updateState({ number: paymentNum }));
				optionsRef.current.paymentNum = paymentNum;
				uniquePaymentNumberRef.current = paymentNum;
			} else {
				var _optionsRef$current2;
				uniquePaymentNumberRef.current = (_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : _optionsRef$current2.paymentNum;
			}
			optionsRef.current.orderId = orderIdRef.current;
			optionsRef.current.deviceId = (_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.id;
			console.log("optionsRef.current =>>>> ", optionsRef.current);
			paymentMethodRef.current = new PaymentMethod(optionsRef.current);
		}
	};
	useEffect(() => {
		return () => {
			var _paymentMethodRef$cur, _paymentMethodRef$cur2;
			(_paymentMethodRef$cur = paymentMethodRef.current) === null || _paymentMethodRef$cur === void 0 || (_paymentMethodRef$cur2 = _paymentMethodRef$cur.destroy) === null || _paymentMethodRef$cur2 === void 0 || _paymentMethodRef$cur2.call(_paymentMethodRef$cur);
			paymentMethodRef.current = null;
		};
	}, []);
	const run = (runParams) => {
		var _optionsRef$current3, _payRef$current, _deviceRef$current2, _options$config;
		initPayment();
		let params = {
			order_id: orderIdRef.current,
			unique_payment_number: uniquePaymentNumberRef.current,
			payment_code: (_optionsRef$current3 = optionsRef.current) === null || _optionsRef$current3 === void 0 ? void 0 : _optionsRef$current3.paymentCode,
			amount: isPay ? (_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.total : amountRef.current,
			operator_id: custom === null || custom === void 0 ? void 0 : custom.operator_id,
			operator_type: custom === null || custom === void 0 ? void 0 : custom.operator_type,
			platform: custom === null || custom === void 0 ? void 0 : custom.platform,
			eftpos: {
				card_reader_type: eftposRef.current,
				card_reader_id: (_deviceRef$current2 = deviceRef.current) === null || _deviceRef$current2 === void 0 ? void 0 : _deviceRef$current2.id,
				pay_param_base64: encodeURIComponent(btoa(encodeURIComponent(JSON.stringify(_objectSpread2(_objectSpread2({}, dataRef.current), {}, { number: uniquePaymentNumberRef.current })))))
			}
		};
		if (options === null || options === void 0 || (_options$config = options.config) === null || _options$config === void 0 ? void 0 : _options$config.requestDelaySeconds) params.delay_seconds = options.config.requestDelaySeconds;
		if (isPay) {
			var _payRef$current2, _payRef$current3, _payRef$current4, _paymentMethodRef$cur3;
			params.payment = {
				original_amount: (_payRef$current2 = payRef.current) === null || _payRef$current2 === void 0 ? void 0 : _payRef$current2.amount,
				order_payment_type: "normal",
				custom_payment_id: custom === null || custom === void 0 ? void 0 : custom.custom_payment_id,
				custom_payment_code: custom === null || custom === void 0 ? void 0 : custom.custom_payment_code,
				custom_payment_name: custom === null || custom === void 0 ? void 0 : custom.custom_payment_name,
				custom_payment_type: custom === null || custom === void 0 ? void 0 : custom.custom_payment_type,
				service_charge: {
					amount: (_payRef$current3 = payRef.current) === null || _payRef$current3 === void 0 ? void 0 : _payRef$current3.surMoney,
					percentage: (_payRef$current4 = payRef.current) === null || _payRef$current4 === void 0 ? void 0 : _payRef$current4.surPercentRate
				},
				metadata: { unique_payment_number: uniquePaymentNumberRef.current }
			};
			if (runParams === null || runParams === void 0 ? void 0 : runParams.params) params = _objectSpread2(_objectSpread2({}, params), runParams.params || {});
			(_paymentMethodRef$cur3 = paymentMethodRef.current) === null || _paymentMethodRef$cur3 === void 0 || _paymentMethodRef$cur3.pay(params, runParams === null || runParams === void 0 ? void 0 : runParams.config);
		} else {
			var _paymentMethodRef$cur4;
			if (runParams === null || runParams === void 0 ? void 0 : runParams.params) params = _objectSpread2(_objectSpread2({}, params), runParams.params || {});
			(_paymentMethodRef$cur4 = paymentMethodRef.current) === null || _paymentMethodRef$cur4 === void 0 || _paymentMethodRef$cur4.refund(params, runParams === null || runParams === void 0 ? void 0 : runParams.config);
		}
	};
	const query = () => {
		initPayment();
		if (isPay) {
			var _paymentMethodRef$cur5;
			(_paymentMethodRef$cur5 = paymentMethodRef.current) === null || _paymentMethodRef$cur5 === void 0 || _paymentMethodRef$cur5.payQuery();
		} else {
			var _paymentMethodRef$cur6;
			(_paymentMethodRef$cur6 = paymentMethodRef.current) === null || _paymentMethodRef$cur6 === void 0 || _paymentMethodRef$cur6.refundQuery();
		}
	};
	const action = (data, config) => {
		var _optionsRef$current4;
		initPayment();
		const params = {
			order_id: orderIdRef.current,
			unique_payment_number: uniquePaymentNumberRef.current,
			payment_code: (_optionsRef$current4 = optionsRef.current) === null || _optionsRef$current4 === void 0 ? void 0 : _optionsRef$current4.paymentCode,
			data
		};
		if (isPay) {
			var _paymentMethodRef$cur7;
			(_paymentMethodRef$cur7 = paymentMethodRef.current) === null || _paymentMethodRef$cur7 === void 0 || _paymentMethodRef$cur7.payAction(params, config);
		} else {
			var _paymentMethodRef$cur8;
			(_paymentMethodRef$cur8 = paymentMethodRef.current) === null || _paymentMethodRef$cur8 === void 0 || _paymentMethodRef$cur8.refundAction(params, config);
		}
	};
	/**
	* 重置支付实例，清空交易号，用于取消后重新发起支付
	* 调用后 paymentMethodRef.current 置空，下次 run 会创建全新实例和新交易号
	*/
	const reset = () => {
		var _paymentMethodRef$cur9, _paymentMethodRef$cur10;
		(_paymentMethodRef$cur9 = paymentMethodRef.current) === null || _paymentMethodRef$cur9 === void 0 || (_paymentMethodRef$cur10 = _paymentMethodRef$cur9.stop) === null || _paymentMethodRef$cur10 === void 0 || _paymentMethodRef$cur10.call(_paymentMethodRef$cur9);
		paymentMethodRef.current = null;
		uniquePaymentNumberRef.current = null;
		optionsRef.current.paymentNum = "";
	};
	return {
		run,
		query,
		action,
		reset,
		_instance: paymentMethodRef.current
	};
};
//#endregion
export { usePayment };
