'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var chunkLDTMSHUZ_cjs = require('./chunk-LDTMSHUZ.cjs');

// ../../node_modules/.pnpm/delay@6.0.0/node_modules/delay/index.js
chunkLDTMSHUZ_cjs.init_cjs_shims();
var randomInteger = (minimum, maximum) => Math.floor(Math.random() * (maximum - minimum + 1) + minimum);
var createAbortError = () => {
  const error = new Error("Delay aborted");
  error.name = "AbortError";
  return error;
};
var clearMethods = /* @__PURE__ */ new WeakMap();
function createDelay({
  clearTimeout: defaultClear,
  setTimeout: defaultSet
} = {}) {
  return (milliseconds, {
    value,
    signal
  } = {}) => {
    if (signal?.aborted) {
      return Promise.reject(createAbortError());
    }
    let timeoutId;
    let settle;
    let rejectFunction;
    const clear = defaultClear ?? clearTimeout;
    const signalListener = () => {
      clear(timeoutId);
      rejectFunction(createAbortError());
    };
    const cleanup = () => {
      if (signal) {
        signal.removeEventListener("abort", signalListener);
      }
    };
    const delayPromise = new Promise((resolve, reject) => {
      settle = () => {
        cleanup();
        resolve(value);
      };
      rejectFunction = reject;
      timeoutId = (defaultSet ?? setTimeout)(settle, milliseconds);
    });
    if (signal) {
      signal.addEventListener("abort", signalListener, {
        once: true
      });
    }
    clearMethods.set(delayPromise, () => {
      clear(timeoutId);
      timeoutId = null;
      settle();
    });
    return delayPromise;
  };
}
var delay = createDelay();
var delay_default = delay;
async function rangeDelay(minimum, maximum, options = {}) {
  return delay(randomInteger(minimum, maximum), options);
}
function clearDelay(promise) {
  clearMethods.get(promise)?.();
}

exports.clearDelay = clearDelay;
exports.createDelay = createDelay;
exports.default = delay_default;
exports.rangeDelay = rangeDelay;
//# sourceMappingURL=delay-W2TSML2P.cjs.map
//# sourceMappingURL=delay-W2TSML2P.cjs.map