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/useMicropay.ts
const useMicropay = (options) => {
	const { custom } = useContextData();
	const { modeRef, payRef, orderIdRef, eftposRef, deviceRef, amountRef } = useStoreRef();
	const paymentMethodRef = useRef(null);
	const dispatch = useDispatch();
	const uniquePaymentNumberRef = useRef(null);
	const isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
	useEffect(() => {
		if (!paymentMethodRef.current) {
			var _deviceRef$current;
			if (!options.paymentNum) {
				const paymentNum = getUuid();
				dispatch(updateState({ number: paymentNum }));
				options.paymentNum = paymentNum;
				uniquePaymentNumberRef.current = paymentNum;
			} else uniquePaymentNumberRef.current = options.paymentNum;
			options.orderId = orderIdRef.current;
			options.deviceId = (_deviceRef$current = deviceRef.current) === null || _deviceRef$current === void 0 ? void 0 : _deviceRef$current.id;
			paymentMethodRef.current = new PaymentMethod(options);
		}
		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 _payRef$current, _deviceRef$current2;
		const params = {
			order_id: orderIdRef.current,
			unique_payment_number: uniquePaymentNumberRef.current,
			payment_code: options.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,
			micropay: {
				card_reader_type: eftposRef.current,
				card_reader_id: (_deviceRef$current2 = deviceRef.current) === null || _deviceRef$current2 === void 0 ? void 0 : _deviceRef$current2.id
			}
		};
		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 }
			};
			params.micropay.auth_code = (runParams === null || runParams === void 0 ? void 0 : runParams.authCode) || "";
			(_paymentMethodRef$cur3 = paymentMethodRef.current) === null || _paymentMethodRef$cur3 === void 0 || _paymentMethodRef$cur3.pay(params);
		} else {
			var _paymentMethodRef$cur4;
			params.micropay.order_payment_id = (runParams === null || runParams === void 0 ? void 0 : runParams.orderPaymentId) || "";
			(_paymentMethodRef$cur4 = paymentMethodRef.current) === null || _paymentMethodRef$cur4 === void 0 || _paymentMethodRef$cur4.refund(params);
		}
	};
	const query = () => {
		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) => {
		const params = {
			order_id: orderIdRef.current,
			unique_payment_number: uniquePaymentNumberRef.current,
			payment_code: options.paymentCode,
			data
		};
		if (isPay) {
			var _paymentMethodRef$cur7;
			(_paymentMethodRef$cur7 = paymentMethodRef.current) === null || _paymentMethodRef$cur7 === void 0 || _paymentMethodRef$cur7.payAction(params);
		} else {
			var _paymentMethodRef$cur8;
			(_paymentMethodRef$cur8 = paymentMethodRef.current) === null || _paymentMethodRef$cur8 === void 0 || _paymentMethodRef$cur8.refundAction(params);
		}
	};
	return {
		run,
		query,
		action
	};
};
//#endregion
export { useMicropay as default };
