"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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);
var retry_exports = {};
__export(retry_exports, {
  pRetry: () => pRetry
});
module.exports = __toCommonJS(retry_exports);
var import_sentinels = require("../constants/sentinels");
var import_math = require("../primordials/math");
var import_internal = require("./_internal");
var import_options = require("./options");
// @__NO_SIDE_EFFECTS__
async function pRetry(callbackFn, options) {
  const {
    args,
    backoffFactor,
    baseDelayMs,
    jitter,
    maxDelayMs,
    onRetry,
    onRetryCancelOnFalse,
    onRetryRethrow,
    retries,
    signal
  } = (0, import_options.normalizeRetryOptions)(options);
  if (signal?.aborted) {
    return void 0;
  }
  if (retries === 0) {
    return await callbackFn(...args || [], { signal });
  }
  const timers = (0, import_internal.getTimers)();
  let attempts = retries;
  let delay = baseDelayMs;
  let error = import_sentinels.UNDEFINED_TOKEN;
  while (attempts-- >= 0) {
    if (signal?.aborted) {
      return void 0;
    }
    try {
      return await callbackFn(...args || [], { signal });
    } catch (e) {
      error = e;
      if (attempts < 0) {
        break;
      }
      let waitTime = delay;
      if (jitter) {
        waitTime += (0, import_math.MathFloor)((0, import_math.MathRandom)() * delay);
      }
      waitTime = (0, import_math.MathMin)(waitTime, maxDelayMs);
      if (typeof onRetry === "function") {
        try {
          const result = onRetry(retries - attempts, e, waitTime);
          if (result === false && onRetryCancelOnFalse) {
            break;
          }
          if (typeof result === "number" && result >= 0) {
            waitTime = (0, import_math.MathMin)(result, maxDelayMs);
          }
        } catch (e2) {
          if (onRetryRethrow) {
            throw e2;
          }
        }
      }
      try {
        await timers.setTimeout(waitTime, void 0, { signal });
      } catch {
        return void 0;
      }
      if (signal?.aborted) {
        return void 0;
      }
      delay = (0, import_math.MathMin)(delay * backoffFactor, maxDelayMs);
    }
  }
  if (error !== import_sentinels.UNDEFINED_TOKEN) {
    throw error;
  }
  return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  pRetry
});
