require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_const = require("../const.js");
const require_helper = require("../helper.js");
const require_payLog = require("../utils/payLog.js");
let react = require("react");
//#region src/components/eftposPay/payo/hooks.tsx
/**
* @title: 循环查询 接口状态
* @Author: hongbing.wang
* @Date: 2024-08-20 13:29
*/
const useValidate = (timeout) => {
	const controllerRef = (0, react.useRef)();
	const ref = (0, react.useRef)(true);
	const isTimeoutCloseRef = (0, react.useRef)(false);
	const TimeoutRef = (0, react.useRef)();
	const checkApi = (0, react.useCallback)(function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* (check, number, time) {
			time = time || 5e3;
			controllerRef.current = new AbortController();
			let res = { code: require_const.PayStatus.Timeout };
			console.log("循环开始前...");
			while (ref.current && res && `${res.code}` === require_const.PayStatus.Timeout) {
				const start = Date.now();
				res = yield check({ number }, { signal: controllerRef.current.signal }).catch((err) => Promise.resolve(err));
				const diff = Date.now() - start;
				console.log(diff, time, time - diff, "diff", res);
				if (diff < time) yield require_helper.sleep(time - diff);
			}
			if (isTimeoutCloseRef.current) {
				if (!res) return { code: require_const.PayStatus.Timeout };
				const { code } = res;
				if (`${code}` === `200`) return require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, res), {}, { code: require_const.PayStatus.Timeout });
			}
			return res;
		});
		return function(_x, _x2, _x3) {
			return _ref.apply(this, arguments);
		};
	}(), []);
	/**
	* @title: 关闭循环
	* @Author: hongbing.wang
	* @Date: 2024-08-20 13:48
	*/
	const close = (0, react.useCallback)(() => {
		var _controllerRef$curren;
		ref.current = false;
		(_controllerRef$curren = controllerRef.current) === null || _controllerRef$curren === void 0 || _controllerRef$curren.abort();
	}, []);
	const run = (...params) => {
		isTimeoutCloseRef.current = false;
		TimeoutRef.current && clearTimeout(TimeoutRef.current);
		TimeoutRef.current = setTimeout(() => {
			isTimeoutCloseRef.current = true;
			require_payLog.default.log("Payo：支付状态查询超时");
			close();
		}, timeout);
		return checkApi(...params);
	};
	(0, react.useEffect)(() => () => {
		clearTimeout(TimeoutRef.current);
		close();
	}, [close]);
	return [run, close];
};
//#endregion
exports.useValidate = useValidate;
