import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { ClientEnum, PayStatus } from "./const.js";
import { updateComponent, updateCustom, updateKey, updateState, updateSteps, useContextData, useDispatch } from "./store/index.js";
import { useCallback, useEffect, useRef, useState } from "react";
import { locales } from "@pisell/utils";
//#region src/components/eftposPay/hooks.tsx
const Max4004 = 1;
const useStoreRef = () => {
	const data = useContextData();
	const { order_id, device, pay, number, mode, net, status, symbol, amount, eftpos, client } = data;
	const dataRef = useRef(data);
	const deviceRef = useRef(device);
	const payRef = useRef(pay);
	const numRef = useRef(number);
	const orderIdRef = useRef(order_id);
	const modeRef = useRef(mode);
	const statusRef = useRef(status);
	const netRef = useRef(net);
	const symbolRef = useRef(symbol);
	const amountRef = useRef(amount);
	const eftposRef = useRef(eftpos);
	const clientRef = useRef(client);
	useEffect(() => {
		dataRef.current = data;
	}, [data]);
	useEffect(() => {
		deviceRef.current = device;
	}, [device]);
	useEffect(() => {
		payRef.current = pay;
	}, [pay]);
	useEffect(() => {
		numRef.current = number;
	}, [number]);
	useEffect(() => {
		orderIdRef.current = order_id;
	}, [order_id]);
	useEffect(() => {
		modeRef.current = mode;
	}, [mode]);
	useEffect(() => {
		statusRef.current = status;
	}, [status]);
	useEffect(() => {
		netRef.current = net;
	}, [net]);
	useEffect(() => {
		symbolRef.current = symbol;
	}, [symbol]);
	useEffect(() => {
		amountRef.current = amount;
	}, [amount]);
	useEffect(() => {
		eftposRef.current = eftpos;
	}, [eftpos]);
	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 } = useContextData();
	const { modeRef, clientRef } = useStoreRef();
	const dispatch = useDispatch();
	const ref = useRef((custom === null || custom === void 0 ? void 0 : custom.retry) || 0);
	useEffect(() => {
		ref.current = (custom === null || custom === void 0 ? void 0 : custom.retry) || 0;
	}, [custom === null || custom === void 0 ? void 0 : custom.retry]);
	return useCallback((res, isMark = false) => {
		if (!res || !res.code) {
			dispatch(updateCustom({ errorStatus: "unknow" }));
			dispatch(updateState({ status: "fail" }));
			if (clientRef.current === ClientEnum.Merchant) dispatch(updateComponent("UnknowFail"));
			else dispatch(updateComponent("Prompt"));
			return;
		}
		const [code] = [`${res.code}`];
		console.log("code---->>>>>>>", code);
		if (code === PayStatus.Unknown) {
			steps[index].title = locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.PayTimeout) {
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail.timeout");
			steps[index].title = locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.Timeout) {
			if (isMark) {
				if (clientRef.current === ClientEnum.User) {
					steps[index].status = "fail";
					steps[index].title = locales.getText("page.eftpos-pay.text.timedout.title");
					steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.other");
					steps[index].custom = steps[index].custom ? _objectSpread2(_objectSpread2({}, steps[index].custom), {}, { isPosNetworkError: true }) : { isPosNetworkError: true };
					dispatch(updateSteps(steps));
					dispatch(updateComponent("Fail"));
					dispatch(updateCustom({ errorStatus: "know" }));
					return;
				}
				steps[index].status = "fail";
				dispatch(updateSteps(steps));
				dispatch(updateState({ manual: {
					title: locales.getText("page.eftpos-pay.text.timedout.title"),
					text: locales.getText("page.eftpos-pay.text.timedout.text")(modeRef.current)
				} }));
				dispatch(updateCustom({ errorStatus: "know" }));
				if (clientRef.current === ClientEnum.Merchant) dispatch(updateComponent("Manual"));
				else dispatch(updateComponent("Prompt"));
				return;
			}
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.pay.fail.timeout");
			steps[index].title = locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.NoPay) {
			if (ref.current > Max4004) {
				dispatch(updateState({ number: "" }));
				dispatch(updateCustom({ retry: ref.current }));
				steps[index].title = locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
				steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail");
				steps[index].status = "fail";
				dispatch(updateSteps(steps));
				dispatch(updateComponent("Fail"));
				return;
			}
			ref.current += 1;
			dispatch(updateState({ number: "" }));
			dispatch(updateCustom({ retry: ref.current }));
			dispatch(updateKey(Date.now()));
			return;
		}
		if (code === PayStatus.PayOtherEftposFailed) {
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.fail.terminal.title");
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail.terminal");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.PayCardErrorFailed) {
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.fail.cancel.title");
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail.cancel");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.NoNetWork || code === PayStatus.SocketError) {
			steps[index].title = locales.getText("page.eftpos-pay.text.interrupted");
			steps[index].warn = clientRef.current === ClientEnum.Merchant ? `${locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.title")}\n${locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.power")}\n${locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.device")}\n${locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.network")}\n${locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.if")(modeRef.current)}` : locales.getText("page.eftpos-pay.text.transaction.eftpos.fail.other");
			steps[index].status = "warn";
			steps[index].custom = steps[index].custom ? _objectSpread2(_objectSpread2({}, steps[index].custom), {}, { isPosNetworkError: true }) : { isPosNetworkError: true };
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			if (clientRef.current === ClientEnum.Merchant) dispatch(updateComponent("Fail"));
			else dispatch(updateComponent("Prompt"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.PayBankRefuses) {
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.back.declined.text");
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.back.declined.title");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.SignatureDeclined) {
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.user.declined.text");
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.user.declined.title");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.UserConfigError || code === PayStatus.PairingFailure) {
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.user.config.title");
			steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.user.config.text");
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		if (code === PayStatus.PaymentFailed) {
			steps[index].title = locales.getText("page.eftpos-pay.text.transaction.fail");
			steps[index].fail = (res === null || res === void 0 ? void 0 : res.message) || locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
			steps[index].status = "fail";
			dispatch(updateSteps(steps));
			dispatch(updateComponent("Fail"));
			dispatch(updateCustom({ errorStatus: "know" }));
			return;
		}
		steps[index].title = locales.getText("page.eftpos-pay.text.failed")(modeRef.current);
		steps[index].status = "fail";
		steps[index].fail = locales.getText("page.eftpos-pay.text.transaction.fail");
		dispatch(updateSteps(steps));
		dispatch(updateCustom({ errorStatus: "unknow" }));
		dispatch(updateComponent("UnknowFail"));
	}, [
		dispatch,
		index,
		steps
	]);
};
const useCountdown = (initialTime) => {
	const [time, setTime] = useState(initialTime);
	const [isRunning, setIsRunning] = useState(false);
	const animationFrameId = useRef();
	const runningRef = useRef(false);
	const prevTimeRef = useRef(0);
	const updateCountdown = 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 = useCallback(() => {
		setTime(initialTime);
		setIsRunning(true);
		prevTimeRef.current = 0;
		runningRef.current = true;
		animationFrameId.current = requestAnimationFrame(updateCountdown);
	}, [initialTime]);
	const cancel = useCallback(() => {
		runningRef.current = false;
		setIsRunning(false);
		if (animationFrameId.current) cancelAnimationFrame(animationFrameId.current);
	}, []);
	useEffect(() => {
		return () => {
			if (animationFrameId.current) cancelAnimationFrame(animationFrameId.current);
		};
	}, []);
	return {
		time,
		isRunning,
		start,
		cancel
	};
};
//#endregion
export { useCountdown, useFail, useStoreRef };
