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_const$1 = require("./const.js");
const require_index = require("../store/index.js");
const require_hooks = require("../hooks.js");
const require_timer = require("./timer.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
//#region src/components/eftposPay/windcave/helper.tsx
const Max4004 = 1;
/**
* @title: windcave 支付,退款,查询 错误逻辑处理
* @description: 动态步骤页 异常前往 FailCustom 页面
* @return null
* @Author: hongbing.wang
* @Date: 2024-08-12 10:29
*/
const useFail = () => {
	const dispatch = require_index.useDispatch();
	const { custom } = require_index.useContextData();
	const { modeRef, clientRef } = require_hooks.useStoreRef();
	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}`];
		if (`${res.code}` === require_const.PayStatus.Unknown) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail"),
				warn: null,
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current)
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		console.log(code, ref.current, Max4004);
		if (code === require_const.PayStatus.NoPay) {
			if (ref.current > Max4004) {
				dispatch(require_index.updateCustom({
					retry: 0,
					failCustom: {
						isPosNetworkError: false,
						status: "fail",
						fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail"),
						warn: null,
						titl: _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current)
					}
				}));
				dispatch(require_index.updateComponent("FailCustom"));
				return;
			}
			ref.current += 1;
			dispatch(require_index.updateState({ number: "" }));
			dispatch(require_index.updateCustom({
				retry: ref.current,
				list: []
			}));
			dispatch(require_index.updateKey(Date.now()));
			return;
		}
		if (code === require_const.PayStatus.PayTimeout) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.timeout"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		if (code === require_const.PayStatus.Timeout) {
			if (isMark) {
				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)
				} }));
				if (clientRef.current === require_const.ClientEnum.Merchant) dispatch(require_index.updateComponent("Manual"));
				else dispatch(require_index.updateComponent("Prompt"));
				return;
			}
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.timeout"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.failed")(modeRef.current),
				warn: null
			} }));
			dispatch(require_index.updateComponent("Prompt"));
			return;
		}
		if (code === require_const.PayStatus.PayOtherEftposFailed) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.terminal"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.terminal.title"),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		if (code === require_const.PayStatus.PayCardErrorFailed) {
			dispatch(require_index.updateCustom({ failCustom: {
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.fail.cancel.title"),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		if (code === require_const.PayStatus.NoNetWork || code === require_const.PayStatus.SocketError) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: true,
				status: "warn",
				fail: null,
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.interrupted"),
				warn: `${_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)}`
			} }));
			if (clientRef.current === require_const.ClientEnum.Merchant) dispatch(require_index.updateComponent("FailCustom"));
			else dispatch(require_index.updateComponent("Prompt"));
			return;
		}
		if (code === require_const.PayStatus.PayBankRefuses) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.back.declined.text"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.back.declined.title"),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		if (code === require_const.PayStatus.SignatureDeclined) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.declined.text"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.declined.title"),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		if (code === require_const.PayStatus.UserConfigError) {
			dispatch(require_index.updateCustom({ failCustom: {
				isPosNetworkError: false,
				status: "fail",
				fail: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.config.text"),
				title: _pisell_utils.locales.getText("page.eftpos-pay.text.transaction.user.config.title"),
				warn: null
			} }));
			dispatch(require_index.updateComponent("FailCustom"));
			return;
		}
		dispatch(require_index.updateCustom({ errorStatus: "unknow" }));
		dispatch(require_index.updateComponent("UnknowFail"));
	}, [dispatch]);
};
/**
* @title: 轮询 check 接口
* @description: 轮询 终止条件 1.超时 , 2. 返回非200状态码 , 3. 外部关闭
* @return [run, XhrTimer] run: 启动函数 , XhrTimer 轮询类
* @Author: hongbing.wang
* @Date: 2024-08-12 10:30
*/
const useValidate = (check, num, onChange) => {
	const unloadRef = (0, react.useRef)(false);
	const dispatch = require_index.useDispatch();
	const failRef = (0, react.useRef)(useFail());
	const ref = (0, react.useRef)();
	const { custom } = require_index.useContextData();
	const { netRef, payRef, numRef, modeRef, clientRef } = require_hooks.useStoreRef();
	const stepRef = (0, react.useRef)(custom === null || custom === void 0 ? void 0 : custom.step);
	const listRef = (0, react.useRef)(custom === null || custom === void 0 ? void 0 : custom.list);
	(0, react.useEffect)(() => {
		listRef.current = custom === null || custom === void 0 ? void 0 : custom.list;
	}, [custom === null || custom === void 0 ? void 0 : custom.list]);
	(0, react.useEffect)(() => {
		stepRef.current = custom === null || custom === void 0 ? void 0 : custom.step;
	}, [custom === null || custom === void 0 ? void 0 : custom.step]);
	const watchRef = (0, react.useRef)();
	const run = (0, react.useCallback)((timeout) => {
		var _ref$current2, _ref$current3;
		if (timeout === -1) console.log("无限加载");
		else console.log("计时加载 ", typeof timeout === "number" ? timeout : num);
		if (unloadRef.current) {
			var _ref$current;
			(_ref$current = ref.current) === null || _ref$current === void 0 || _ref$current.close();
			return;
		}
		if (!watchRef.current || !netRef.current) return;
		(_ref$current2 = ref.current) === null || _ref$current2 === void 0 || _ref$current2.close();
		ref.current = new require_timer.default({
			xhr: check,
			timeout: typeof timeout === "number" ? timeout : num,
			callback: watchRef.current
		});
		(_ref$current3 = ref.current) === null || _ref$current3 === void 0 || _ref$current3.run({ number: numRef.current }).then((res) => {
			if (unloadRef.current) return;
			if (!netRef.current) return;
			if (!res || Number(res.code) !== 200) return Promise.reject(res);
		}).catch((res) => {
			var _failRef$current;
			if (unloadRef.current) return;
			if (!netRef.current) return;
			if (res && res.code === -1) return;
			(_failRef$current = failRef.current) === null || _failRef$current === void 0 || _failRef$current.call(failRef, res, stepRef.current >= require_const$1.StepStatusEnum.Account);
		});
	}, [check, num]);
	(0, react.useEffect)(() => {
		/**
		* @title: 轮询结果处理
		* @description: 将轮询结果进行处理,根据不同的轮询结果进行不同的业务逻辑处理
		* @param {any} res 轮询接口范湖遗址
		* @return null
		* @Author: hongbing.wang
		* @Date: 2024-08-12 12:42
		*/
		watchRef.current = (res, isRun) => {
			if (unloadRef.current) {
				var _ref$current4;
				(_ref$current4 = ref.current) === null || _ref$current4 === void 0 || _ref$current4.close();
				return;
			}
			if (!res || !res.data || !res.data.txn_status_id) {
				var _ref$current5;
				(_ref$current5 = ref.current) === null || _ref$current5 === void 0 || _ref$current5.close();
			}
			const [txn_status_id, status_id, complete] = [
				Number(res.data.txn_status_id),
				Number(res.data.status_id),
				Number(res.data.complete)
			];
			if (txn_status_id === require_const$1.StepStatusEnum.Wait && status_id === 6 && complete === 1) {
				var _ref$current6;
				(_ref$current6 = ref.current) === null || _ref$current6 === void 0 || _ref$current6.close();
				if (stepRef.current !== txn_status_id && listRef.current.every(({ index }) => txn_status_id !== index)) {
					var _payRef$current;
					const current = listRef.current.map((item) => {
						if (Number(item === null || item === void 0 ? void 0 : item.index) < Number(stepRef.current)) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { status: "resolve" });
						return item;
					});
					const nextStep = require_const$1.getStepMapping(txn_status_id, {
						money: (_payRef$current = payRef.current) === null || _payRef$current === void 0 ? void 0 : _payRef$current.totalUnit,
						mode: modeRef.current
					});
					if (nextStep) current.push(nextStep);
					dispatch(require_index.updateCustom({
						list: current,
						step: txn_status_id
					}));
				}
				onChange(res.data);
				return;
			}
			if (stepRef.current !== txn_status_id && listRef.current.every(({ index }) => txn_status_id !== index)) {
				var _ref$current7, _payRef$current2;
				console.log("self.flag  stepRef.current, txn_status_id ", stepRef.current, txn_status_id);
				stepRef.current = txn_status_id;
				(_ref$current7 = ref.current) === null || _ref$current7 === void 0 || _ref$current7.close();
				const current = listRef.current.map((item) => {
					if (Number(item === null || item === void 0 ? void 0 : item.index) < Number(stepRef.current)) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), {}, { status: "resolve" });
					return item;
				});
				const nextStep = require_const$1.getStepMapping(txn_status_id, {
					money: (_payRef$current2 = payRef.current) === null || _payRef$current2 === void 0 ? void 0 : _payRef$current2.totalUnit,
					mode: modeRef.current
				});
				if (nextStep) {
					current.push(nextStep);
					if (nextStep.type === require_const$1.StemTypeEnum.Passive) dispatch(require_index.updateStatus(require_const.StatusEnum.Question));
					if (nextStep.index === require_const$1.StepStatusEnum.Receipt) dispatch(require_index.updateState({ title: _pisell_utils.locales.getText("page.eftpos-pay.text.waiting.signature") }));
				}
				dispatch(require_index.updateCustom({
					list: current,
					step: txn_status_id
				}));
				txn_status_id === require_const$1.StepStatusEnum.Receipt ? run(-1) : run();
				return;
			}
			if (isRun) run();
		};
	}, [
		dispatch,
		onChange,
		run
	]);
	(0, react.useEffect)(() => () => {
		var _ref$current8;
		(_ref$current8 = ref.current) === null || _ref$current8 === void 0 || _ref$current8.close();
		unloadRef.current = true;
	}, []);
	return [
		run,
		watchRef,
		ref
	];
};
//#endregion
exports.useFail = useFail;
exports.useValidate = useValidate;
