import { ModeEnum, PayStatus, StatusEnum } from "../../const.js";
import { LinklyActionStatus } from "../const.js";
import { updateCustom, updateState, updateStatus, useDispatch } from "../../store/index.js";
import { useFail, useStoreRef } from "../../hooks.js";
import payLog from "../../utils/payLog.js";
import { linklyPayApi, linklyPayCancelApi, linklyPaySignatureApi, linklyRefundApi, linklyRefundCancelApi, linklyRefundSignatureApi } from "../service.js";
import { useEffect, useRef } from "react";
import { locales } from "@pisell/utils";
import { useRequest } from "ahooks";
//#region src/components/eftposPay/linkly/hooks/normal.ts
/**
* 支付或退款
* @param failAction 失败回调
*/
const useLinklyPayOrRefund = (handleCheck) => {
	const dispatch = useDispatch();
	const { numRef, modeRef, orderIdRef, deviceRef, payRef, amountRef } = useStoreRef();
	const fail = useFail(0);
	const failRef = useRef(fail);
	const resultQueryRef = useRef(null);
	const handleLinklySuccessRef = useRef(null);
	useEffect(() => {
		failRef.current = fail;
	}, [fail]);
	const isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
	const _pay = useRequest(isPay ? linklyPayApi : linklyRefundApi, {
		manual: true,
		onSuccess: (res) => {
			onSuccess(res);
		},
		onError: (error) => {
			onError(error);
		}
	});
	const onSuccess = (res) => {
		var _res$data;
		console.log(`linkly${isPay ? "Pay" : "Refund"}Success---->>>>>>>`, res);
		if (!handleCheck()) return;
		payLog.log(`Linkly：${isPay ? "支付" : "退款"}接口成功`, { res });
		const status = res === null || res === void 0 || (_res$data = res.data) === null || _res$data === void 0 ? void 0 : _res$data.status;
		if (status == 0) {
			var _res$data2;
			numRef.current = res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.session_id;
			dispatch(updateState({ number: numRef.current }));
			dispatch(updateCustom({ actionStatus: LinklyActionStatus.Cancel }));
			setTimeout(() => {
				var _resultQueryRef$curre;
				(_resultQueryRef$curre = resultQueryRef.current) === null || _resultQueryRef$curre === void 0 || _resultQueryRef$curre.call(resultQueryRef);
			}, 300);
			return;
		}
		dispatch(updateCustom({ actionStatus: LinklyActionStatus.Empty }));
		if (status == 1) {
			var _handleLinklySuccessR;
			(_handleLinklySuccessR = handleLinklySuccessRef.current) === null || _handleLinklySuccessR === void 0 || _handleLinklySuccessR.call(handleLinklySuccessRef, res);
			return;
		}
		if (status < 0) {
			var _failRef$current;
			res.code = PayStatus.Unknown;
			(_failRef$current = failRef.current) === null || _failRef$current === void 0 || _failRef$current.call(failRef, res, false);
		}
	};
	const onError = (error) => {
		var _failRef$current2;
		console.log(`linkly${isPay ? "Pay" : "Refund"}Error---->>>>>>>`, error);
		if (!handleCheck()) return;
		payLog.log(`Linkly：${isPay ? "支付" : "退款"}接口失败`, { error: JSON.stringify(error || {}) });
		(_failRef$current2 = failRef.current) === null || _failRef$current2 === void 0 || _failRef$current2.call(failRef, error, false);
	};
	const payOrRefund = (resultQuery, handleLinklySuccess) => {
		var _ref, _payRef$current;
		handleLinklySuccessRef.current = handleLinklySuccess;
		resultQueryRef.current = resultQuery;
		const param = {
			order_id: orderIdRef.current,
			pinpad_id: deviceRef === null || deviceRef === void 0 || (_ref = deviceRef.current) === null || _ref === void 0 || (_ref = _ref.metadata) === null || _ref === void 0 ? void 0 : _ref.linkly_pinpad_id,
			amount: isPay ? (_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.total : amountRef.current
		};
		payLog.setContext({ transactionNumber: String(orderIdRef.current) });
		payLog.log(`Linkly：发起${isPay ? "支付" : "退款"}`, { param });
		_pay.run(param);
	};
	return {
		action: { pay: _pay },
		payOrRefund
	};
};
/**
* @title: 取消支付或退款
* @description: 取消支付或退款调用接口后不管成功还是失败都需要调用支付查询接口
*/
const useLinklyCancel = (handleCheck) => {
	const dispatch = useDispatch();
	const { numRef, modeRef, deviceRef } = useStoreRef();
	const resultQueryRef = useRef(null);
	const isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
	const cancelAction = (res, type) => {
		var _resultQueryRef$curre2;
		dispatch(updateCustom({ buttonDisabled: false }));
		if (!handleCheck()) return;
		payLog.log(`Linkly：取消${type === "success" ? "成功" : "失败"}`, { res });
		(_resultQueryRef$curre2 = resultQueryRef.current) === null || _resultQueryRef$curre2 === void 0 || _resultQueryRef$curre2.call(resultQueryRef);
	};
	const _cancel = useRequest(isPay ? linklyPayCancelApi : linklyRefundCancelApi, {
		manual: true,
		onSuccess: (res) => cancelAction(res, "success"),
		onError: (error) => cancelAction(error, "error")
	});
	const linklyCancel = (resultQuery) => {
		var _ref2;
		resultQueryRef.current = resultQuery;
		dispatch(updateCustom({ buttonDisabled: true }));
		const param = {
			pinpad_id: deviceRef === null || deviceRef === void 0 || (_ref2 = deviceRef.current) === null || _ref2 === void 0 || (_ref2 = _ref2.metadata) === null || _ref2 === void 0 ? void 0 : _ref2.linkly_pinpad_id,
			session_id: numRef.current
		};
		payLog.log("Linkly：发起取消", { param });
		_cancel.run(param);
		dispatch(updateCustom({ actionStatus: LinklyActionStatus.CancelWaiting }));
	};
	return {
		action: { cancel: _cancel },
		linklyCancel
	};
};
/**
* @title: 支付或退款签名
* @description: 支付或退款签名调用接口后不管成功还是失败都需要调用支付查询接口
*/
const useLinklySignature = (handleCheck) => {
	const dispatch = useDispatch();
	const { numRef, modeRef, deviceRef } = useStoreRef();
	const resultQueryRef = useRef(null);
	const isPay = modeRef.current === ModeEnum.Pay || modeRef.current === ModeEnum.FullPay;
	const signatureAction = (res, type) => {
		var _resultQueryRef$curre3;
		console.log("signatureAction---->>>>>>>", res);
		if (!handleCheck()) return;
		payLog.log(`Linkly：签名接口${type === "success" ? "成功" : "失败"}`, { res });
		(_resultQueryRef$curre3 = resultQueryRef.current) === null || _resultQueryRef$curre3 === void 0 || _resultQueryRef$curre3.call(resultQueryRef);
	};
	const _signature = useRequest(isPay ? linklyPaySignatureApi : linklyRefundSignatureApi, {
		manual: true,
		onSuccess: (res) => signatureAction(res, "success"),
		onError: (error) => signatureAction(error, "error")
	});
	const linklySignature = (status, resultQuery) => {
		var _ref3;
		resultQueryRef.current = resultQuery;
		const param = {
			pinpad_id: deviceRef === null || deviceRef === void 0 || (_ref3 = deviceRef.current) === null || _ref3 === void 0 || (_ref3 = _ref3.metadata) === null || _ref3 === void 0 ? void 0 : _ref3.linkly_pinpad_id,
			session_id: numRef.current,
			signature_flag: status
		};
		dispatch(updateStatus(StatusEnum.Loading));
		dispatch(updateState({ title: locales.getText("page.eftpos-pay.text.loading")(modeRef.current) }));
		dispatch(updateCustom({ actionStatus: LinklyActionStatus.SignatureWaiting }));
		payLog.log("Linkly：发起签名", { param });
		_signature.run(param);
	};
	return {
		action: { signature: _signature },
		linklySignature
	};
};
//#endregion
export { useLinklyCancel, useLinklyPayOrRefund, useLinklySignature };
