require("../../../../_virtual/_rolldown/runtime.js");
const require_const = require("../../const.js");
const require_const$1 = require("../const.js");
const require_index = require("../../store/index.js");
const require_hooks = require("../../hooks.js");
const require_payLog = require("../../utils/payLog.js");
const require_service = require("../service.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
let ahooks = require("ahooks");
//#region src/components/eftposPay/linkly/hooks/normal.ts
/**
* 支付或退款
* @param failAction 失败回调
*/
const useLinklyPayOrRefund = (handleCheck) => {
	const dispatch = require_index.useDispatch();
	const { numRef, modeRef, orderIdRef, deviceRef, payRef, amountRef } = require_hooks.useStoreRef();
	const fail = require_hooks.useFail(0);
	const failRef = (0, react.useRef)(fail);
	const resultQueryRef = (0, react.useRef)(null);
	const handleLinklySuccessRef = (0, react.useRef)(null);
	(0, react.useEffect)(() => {
		failRef.current = fail;
	}, [fail]);
	const isPay = modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay;
	const _pay = (0, ahooks.useRequest)(isPay ? require_service.linklyPayApi : require_service.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;
		require_payLog.default.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(require_index.updateState({ number: numRef.current }));
			dispatch(require_index.updateCustom({ actionStatus: require_const$1.LinklyActionStatus.Cancel }));
			setTimeout(() => {
				var _resultQueryRef$curre;
				(_resultQueryRef$curre = resultQueryRef.current) === null || _resultQueryRef$curre === void 0 || _resultQueryRef$curre.call(resultQueryRef);
			}, 300);
			return;
		}
		dispatch(require_index.updateCustom({ actionStatus: require_const$1.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 = require_const.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;
		require_payLog.default.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
		};
		require_payLog.default.setContext({ transactionNumber: String(orderIdRef.current) });
		require_payLog.default.log(`Linkly：发起${isPay ? "支付" : "退款"}`, { param });
		_pay.run(param);
	};
	return {
		action: { pay: _pay },
		payOrRefund
	};
};
/**
* @title: 取消支付或退款
* @description: 取消支付或退款调用接口后不管成功还是失败都需要调用支付查询接口
*/
const useLinklyCancel = (handleCheck) => {
	const dispatch = require_index.useDispatch();
	const { numRef, modeRef, deviceRef } = require_hooks.useStoreRef();
	const resultQueryRef = (0, react.useRef)(null);
	const isPay = modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay;
	const cancelAction = (res, type) => {
		var _resultQueryRef$curre2;
		dispatch(require_index.updateCustom({ buttonDisabled: false }));
		if (!handleCheck()) return;
		require_payLog.default.log(`Linkly：取消${type === "success" ? "成功" : "失败"}`, { res });
		(_resultQueryRef$curre2 = resultQueryRef.current) === null || _resultQueryRef$curre2 === void 0 || _resultQueryRef$curre2.call(resultQueryRef);
	};
	const _cancel = (0, ahooks.useRequest)(isPay ? require_service.linklyPayCancelApi : require_service.linklyRefundCancelApi, {
		manual: true,
		onSuccess: (res) => cancelAction(res, "success"),
		onError: (error) => cancelAction(error, "error")
	});
	const linklyCancel = (resultQuery) => {
		var _ref2;
		resultQueryRef.current = resultQuery;
		dispatch(require_index.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
		};
		require_payLog.default.log("Linkly：发起取消", { param });
		_cancel.run(param);
		dispatch(require_index.updateCustom({ actionStatus: require_const$1.LinklyActionStatus.CancelWaiting }));
	};
	return {
		action: { cancel: _cancel },
		linklyCancel
	};
};
/**
* @title: 支付或退款签名
* @description: 支付或退款签名调用接口后不管成功还是失败都需要调用支付查询接口
*/
const useLinklySignature = (handleCheck) => {
	const dispatch = require_index.useDispatch();
	const { numRef, modeRef, deviceRef } = require_hooks.useStoreRef();
	const resultQueryRef = (0, react.useRef)(null);
	const isPay = modeRef.current === require_const.ModeEnum.Pay || modeRef.current === require_const.ModeEnum.FullPay;
	const signatureAction = (res, type) => {
		var _resultQueryRef$curre3;
		console.log("signatureAction---->>>>>>>", res);
		if (!handleCheck()) return;
		require_payLog.default.log(`Linkly：签名接口${type === "success" ? "成功" : "失败"}`, { res });
		(_resultQueryRef$curre3 = resultQueryRef.current) === null || _resultQueryRef$curre3 === void 0 || _resultQueryRef$curre3.call(resultQueryRef);
	};
	const _signature = (0, ahooks.useRequest)(isPay ? require_service.linklyPaySignatureApi : require_service.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(require_index.updateStatus(require_const.StatusEnum.Loading));
		dispatch(require_index.updateState({ title: _pisell_utils.locales.getText("page.eftpos-pay.text.loading")(modeRef.current) }));
		dispatch(require_index.updateCustom({ actionStatus: require_const$1.LinklyActionStatus.SignatureWaiting }));
		require_payLog.default.log("Linkly：发起签名", { param });
		_signature.run(param);
	};
	return {
		action: { signature: _signature },
		linklySignature
	};
};
//#endregion
exports.useLinklyCancel = useLinklyCancel;
exports.useLinklyPayOrRefund = useLinklyPayOrRefund;
exports.useLinklySignature = useLinklySignature;
