import { _defineProperty } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.js";
import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { PayStatus } from "../const.js";
import { getUuid, sleep } from "../helper.js";
//#region src/components/eftposPay/windcave/timer.ts
const MaxTime = 5e3;
/**
* @title: 定时,轮询
* @description:  双层 proxy , 1. 轮询 2. 定时 关闭轮询 ,特殊状态码,前端  701001 超时关闭 -1  外部用户关闭
* @return null
* @Author: hongbing.wang
* @Date: 2024-08-12 11:25
*/
var XhrTimer = class {
	constructor({ xhr, timeout, callback, max = MaxTime }) {
		_defineProperty(this, "clearTimeout", 0);
		_defineProperty(this, "isTimeoutClose", false);
		_defineProperty(this, "timeout", void 0);
		_defineProperty(this, "callback", void 0);
		_defineProperty(this, "flag", true);
		_defineProperty(this, "run", void 0);
		_defineProperty(this, "abortController", void 0);
		_defineProperty(this, "num", void 0);
		this.timeout = timeout;
		this.callback = callback;
		this.num = getUuid();
		const self = this;
		const xhrApi = new Proxy(xhr, { apply(target, ctx, agrns) {
			return _asyncToGenerator(function* () {
				let laster = Date.now();
				let result = yield Promise.resolve(void 0);
				while (self.flag) {
					laster = Date.now();
					if (self.flag) {
						self.abortController = new AbortController();
						if (!agrns[1]) agrns[1] = { signal: self.abortController.signal };
						else agrns[1].signal = self.abortController.signal;
						result = yield Reflect.apply(target, ctx, agrns).then((res) => {
							if (res && `${res.code}` === PayStatus.Success) self.callback(res);
							return res;
						}).catch((err) => {
							console.log("外部取消", self.flag, !self.isTimeoutClose, self.num);
							if (!self.flag && !self.isTimeoutClose) return Promise.resolve({ code: -1 });
							console.log(err);
							self.flag = false;
							return Promise.resolve(err);
						});
					}
					const diff = Date.now() - laster;
					if (self.flag && diff < max) yield sleep(max - diff);
				}
				return result;
			})();
		} });
		this.run = new Proxy(xhrApi, { apply(target, ctx, agrns) {
			return _asyncToGenerator(function* () {
				self.flag = true;
				self.isTimeoutClose = false;
				if (self.clearTimeout) clearTimeout(self.clearTimeout);
				if (self.timeout > 4) self.clearTimeout = setTimeout(() => {
					if (self.flag) {
						self.flag = false;
						self.isTimeoutClose = true;
					}
				}, self.timeout);
				const result = yield Reflect.apply(target, ctx, agrns);
				clearTimeout(self.clearTimeout);
				if (self.isTimeoutClose) {
					if (!result) return { code: PayStatus.Timeout };
					const { code } = result;
					if (`${code}` === `200`) return _objectSpread2(_objectSpread2({}, result), {}, { code: PayStatus.Timeout });
					return result;
				}
				return result;
			})();
		} });
	}
	/**
	* @title: 关闭 循环
	* @Author: hongbing.wang
	* @Date: 2024-08-20 12:48
	*/
	close() {
		var _this$abortController;
		console.log("外部关闭", this.flag, this.num);
		this.flag = false;
		(_this$abortController = this.abortController) === null || _this$abortController === void 0 || _this$abortController.abort();
		if (this.clearTimeout) clearTimeout(this.clearTimeout);
	}
};
//#endregion
export { XhrTimer as default };
