var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/eftposPay/payo/hooks.tsx
var hooks_exports = {};
__export(hooks_exports, {
  useValidate: () => useValidate
});
module.exports = __toCommonJS(hooks_exports);
var import_react = require("react");
var import_const = require("../const");
var import_helper = require("../helper");
var import_payLog = __toESM(require("../utils/payLog"));
var useValidate = (timeout) => {
  const controllerRef = (0, import_react.useRef)();
  const ref = (0, import_react.useRef)(true);
  const isTimeoutCloseRef = (0, import_react.useRef)(false);
  const TimeoutRef = (0, import_react.useRef)();
  const checkApi = (0, import_react.useCallback)(
    async (check, number, time) => {
      time = time || 5e3;
      controllerRef.current = new AbortController();
      let res = {
        code: import_const.PayStatus.Timeout
      };
      console.log("循环开始前...");
      while (ref.current && res && `${res.code}` === import_const.PayStatus.Timeout) {
        const start = Date.now();
        res = await 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) {
          await (0, import_helper.sleep)(time - diff);
        }
      }
      if (isTimeoutCloseRef.current) {
        if (!res) {
          return {
            code: import_const.PayStatus.Timeout
          };
        }
        const { code } = res;
        if (`${code}` === `200`) {
          return {
            ...res,
            code: import_const.PayStatus.Timeout
          };
        }
      }
      return res;
    },
    []
  );
  const close = (0, import_react.useCallback)(() => {
    var _a;
    ref.current = false;
    (_a = controllerRef.current) == null ? void 0 : _a.abort();
  }, []);
  const run = (...params) => {
    isTimeoutCloseRef.current = false;
    TimeoutRef.current && clearTimeout(TimeoutRef.current);
    TimeoutRef.current = setTimeout(() => {
      isTimeoutCloseRef.current = true;
      import_payLog.default.log("Payo：支付状态查询超时");
      close();
    }, timeout);
    return checkApi(...params);
  };
  (0, import_react.useEffect)(
    () => () => {
      clearTimeout(TimeoutRef.current);
      close();
    },
    [close]
  );
  return [run, close];
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useValidate
});
