var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/windcave/timer.ts
var timer_exports = {};
__export(timer_exports, {
  default: () => XhrTimer
});
module.exports = __toCommonJS(timer_exports);
var import_const = require("../const");
var import_helper = require("../helper");
var MaxTime = 5e3;
var XhrTimer = class {
  clearTimeout = 0;
  isTimeoutClose = false;
  timeout;
  callback;
  flag = true;
  run;
  abortController;
  num;
  constructor({
    xhr,
    timeout,
    callback,
    max = MaxTime
  }) {
    this.timeout = timeout;
    this.callback = callback;
    this.num = (0, import_helper.getUuid)();
    const self = this;
    const xhrApi = new Proxy(xhr, {
      async apply(target, ctx, agrns) {
        let laster = Date.now();
        let result = await 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 = await Reflect.apply(target, ctx, agrns).then((res) => {
              if (res && `${res.code}` === import_const.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 currentTime = Date.now();
          const diff = currentTime - laster;
          if (self.flag && diff < max) {
            await (0, import_helper.sleep)(max - diff);
          }
        }
        return result;
      }
    });
    this.run = new Proxy(xhrApi, {
      async apply(target, ctx, agrns) {
        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 = await Reflect.apply(target, ctx, agrns);
        clearTimeout(self.clearTimeout);
        if (self.isTimeoutClose) {
          if (!result) {
            return {
              code: import_const.PayStatus.Timeout
            };
          }
          const { code } = result;
          if (`${code}` === `200`) {
            return {
              ...result,
              code: import_const.PayStatus.Timeout
            };
          }
          return result;
        }
        return result;
      }
    });
  }
  /**
   * @title: 关闭 循环
   * @Author: hongbing.wang
   * @Date: 2024-08-20 12:48
   */
  close() {
    var _a;
    console.log("外部关闭", this.flag, this.num);
    this.flag = false;
    (_a = this.abortController) == null ? void 0 : _a.abort();
    if (this.clearTimeout) {
      clearTimeout(this.clearTimeout);
    }
  }
};
