require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_const = require("./const.js");
const require_index = require("./store/index.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
//#region src/components/eftposPay/hooks.tsx
const Max4004 = 1;
const useStoreRef = () => {
	const data = require_index.useContextData();
	const { order_id, device, pay, number, mode, net, status, symbol, amount, eftpos, client } = data;
	const dataRef = (0, react.useRef)(data);
	const deviceRef = (0, react.useRef)(device);
	const payRef = (0, react.useRef)(pay);
	const numRef = (0, react.useRef)(number);
	const orderIdRef = (0, react.useRef)(order_id);
	const modeRef = (0, react.useRef)(mode);
	const statusRef = (0, react.useRef)(status);
	const netRef = (0, react.useRef)(net);
	const symbolRef = (0, react.useRef)(symbol);
	const amountRef = (0, react.useRef)(amount);
	const eftposRef = (0, react.useRef)(eftpos);
	const clientRef = (0, react.useRef)(client);
	(0, react.useEffect)(() => {
		dataRef.current = data;
	}, [data]);
	(0, react.useEffect)(() => {
		deviceRef.current = device;
	}, [device]);
	(0, react.useEffect)(() => {
		payRef.current = pay;
	}, [pay]);
	(0, react.useEffect)(() => {
		numRef.current = number;
	}, [number]);
	(0, react.useEffect)(() => {
		orderIdRef.current = order_id;
	}, [order_id]);
	(0, react.useEffect)(() => {
		modeRef.current = mode;
	}, [mode]);
	(0, react.useEffect)(() => {
		statusRef.current = status;
	}, [status]);
	(0, react.useEffect)(() => {
		netRef.current = net;
	}, [net]);
	(0, react.useEffect)(() => {
		symbolRef.current = symbol;
	}, [symbol]);
	(0, react.useEffect)(() => {
		amountRef.current = amount;
	}, [amount]);
	(0, react.useEffect)(() => {
		eftposRef.current = eftpos;
	}, [eftpos]);
	(0, react.useEffect)(() => {
		clientRef.current = client;
	}, [client]);
	return {
		deviceRef,
		payRef,
		numRef,
		orderIdRef,
		modeRef,
		statusRef,
		netRef,
		symbolRef,
		amountRef,
		eftposRef,
		clientRef,
		dataRef
	};
};
/**
* @title: 通用失败处理
* @description:  固定步骤 错误处理逻辑
* @return null
* @Author: hongbing.wang
* @Date: 2024-08-12 11:54
*/
const useFail = (index) => {
	const { steps, custom } = require_index.useContextData();
	const { modeRef, clientRef } = useStoreRef();
	const dispatch = require_index.useDispatch();
	const ref = (0, react.useRef)((custom === null || custom === void 0 ? void 0 : custom.retry) || 0);
	(0, react.useEffect)(() => {
		ref.current = (custom === null || custom === void 0 ? void 0 : custom.retry) || 0;
	}, [custom === null || custom === void 0 ? void 0 : custom.retry]);
	return (0, react.useCallback)((res, isMark = false) => {
		if (!res || !res.code) {
			dispatch(require_index.updateCustom({ errorStatus: "unknow" }));
			dispatch(require_index.updateState({ status: "fail" }));
			if (clientRef.current === require_const.ClientEnum.Merchant) dispatch(require_index.updateComponent("UnknowFail"));
			else dispatch(require_index.updateComponent("Prompt"));
			return;
		}
		const [code] = [`${res.code}`];
		console.log("code---->>>>>>>", code);
		if (code === require_const.PayStatus.Unknown) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.PayTimeout) {
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.timeout");
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.Timeout) {
			if (isMark) {
				if (clientRef.current === require_const.ClientEnum.User) {
					steps[index].status = "fail";
					steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.timedout.title");
					steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.other");
					steps[index].custom = steps[index].custom ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, steps[index].custom), {}, { isPosNetworkError: true }) : { isPosNetworkError: true };
					dispatch(require_index.updateSteps(steps));
					dispatch(require_index.updateComponent("Fail"));
					dispatch(require_index.updateCustom({ errorStatus: "know" }));
					return;
				}
				steps[index].status = "fail";
				dispatch(require_index.updateSteps(steps));
				dispatch(require_index.updateState({ manual: {
					title: _pisell_utils.locales.getText("page.eftpos-pay.text.timedout.title"),
					text: _pisell_utils.locales.getText("page.eftpos-pay.text.timedout.text")(modeRef.current)
				} }));
				dispatch(require_index.updateCustom({ errorStatus: "know" }));
				if (clientRef.current === require_const.ClientEnum.Merchant) dispatch(require_index.updateComponent("Manual"));
				else dispatch(require_index.updateComponent("Prompt"));
				return;
			}
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.pay.fail.timeout");
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.NoPay) {
			if (ref.current > Max4004) {
				dispatch(require_index.updateState({ number: "" }));
				dispatch(require_index.updateCustom({ retry: ref.current }));
				steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
				steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail");
				steps[index].status = "fail";
				dispatch(require_index.updateSteps(steps));
				dispatch(require_index.updateComponent("Fail"));
				return;
			}
			ref.current += 1;
			dispatch(require_index.updateState({ number: "" }));
			dispatch(require_index.updateCustom({ retry: ref.current }));
			dispatch(require_index.updateKey(Date.now()));
			return;
		}
		if (code === require_const.PayStatus.PayOtherEftposFailed) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.terminal.title");
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.terminal");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.PayCardErrorFailed) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel.title");
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.NoNetWork || code === require_const.PayStatus.SocketError) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.interrupted");
			steps[index].warn = clientRef.current === require_const.ClientEnum.Merchant ? `${_pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.title")}\n${_pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.power")}\n${_pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.device")}\n${_pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.network")}\n${_pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.if")(modeRef.current)}` : _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.other");
			steps[index].status = "warn";
			steps[index].custom = steps[index].custom ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, steps[index].custom), {}, { isPosNetworkError: true }) : { isPosNetworkError: true };
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			if (clientRef.current === require_const.ClientEnum.Merchant) dispatch(require_index.updateComponent("Fail"));
			else dispatch(require_index.updateComponent("Prompt"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.PayBankRefuses) {
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.back.declined.text");
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.back.declined.title");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.SignatureDeclined) {
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.declined.text");
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.declined.title");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.UserConfigError || code === require_const.PayStatus.PairingFailure) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.config.title");
			steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.config.text");
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === require_const.PayStatus.PaymentFailed) {
			steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail");
			steps[index].fail = (res === null || res === void 0 ? void 0 : res.message) || _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(require_index.updateSteps(steps));
			dispatch(require_index.updateComponent("Fail"));
			dispatch(require_index.updateCustom({ errorStatus: "know" }));
			return;
		}
		steps[index].title = _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
		steps[index].status = "fail";
		steps[index].fail = _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail");
		dispatch(require_index.updateSteps(steps));
		dispatch(require_index.updateCustom({ errorStatus: "unknow" }));
		dispatch(require_index.updateComponent("UnknowFail"));
	}, [
		dispatch,
		index,
		steps
	]);
};
const useCountdown = (initialTime) => {
	const [time, setTime] = (0, react.useState)(initialTime);
	const [isRunning, setIsRunning] = (0, react.useState)(false);
	const animationFrameId = (0, react.useRef)();
	const runningRef = (0, react.useRef)(false);
	const prevTimeRef = (0, react.useRef)(0);
	const updateCountdown = (0, react.useCallback)((timestamp) => {
		if (!runningRef.current) return;
		if (timestamp - prevTimeRef.current >= 1e3) {
			prevTimeRef.current = timestamp;
			setTime((prevTime) => {
				if (prevTime > 0) return prevTime - 1;
				else {
					setIsRunning(false);
					runningRef.current = false;
					return 0;
				}
			});
		}
		animationFrameId.current = requestAnimationFrame(() => {
			animationFrameId.current = requestAnimationFrame(updateCountdown);
		});
	}, []);
	const start = (0, react.useCallback)(() => {
		setTime(initialTime);
		setIsRunning(true);
		prevTimeRef.current = 0;
		runningRef.current = true;
		animationFrameId.current = requestAnimationFrame(updateCountdown);
	}, [initialTime]);
	const cancel = (0, react.useCallback)(() => {
		runningRef.current = false;
		setIsRunning(false);
		if (animationFrameId.current) cancelAnimationFrame(animationFrameId.current);
	}, []);
	(0, react.useEffect)(() => {
		return () => {
			if (animationFrameId.current) cancelAnimationFrame(animationFrameId.current);
		};
	}, []);
	return {
		time,
		isRunning,
		start,
		cancel
	};
};
//#endregion
exports.useCountdown = useCountdown;
exports.useFail = useFail;
exports.useStoreRef = useStoreRef;
