"use strict";
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);
var retry_axios_exports = {};
__export(retry_axios_exports, {
  attach: () => attach,
  detach: () => detach,
  getConfig: () => getConfig
});
module.exports = __toCommonJS(retry_axios_exports);
var import_axios = __toESM(require("axios"));
const onFulfilled = (res) => res;
const detach = (interceptorId, instance) => {
  instance = instance || import_axios.default;
  if (instance?.interceptors?.response)
    instance.interceptors.response.eject(interceptorId);
};
const shouldRetryRequest = (err) => {
  const config = err.config.retryConfig;
  if (!config || config.retry === 0) {
    return false;
  }
  if (!err.response && (config.currentRetryAttempt || 0) >= config.noResponseRetries) {
    return false;
  }
  if (!err.config.method || config.httpMethodsToRetry.indexOf(err.config.method.toUpperCase()) < 0) {
    return false;
  }
  if (err.response && err.response.status) {
    let isInRange = false;
    for (const [min, max] of config.statusCodesToRetry) {
      const { status } = err.response;
      if (status >= min && status <= max) {
        isInRange = true;
        break;
      }
    }
    if (!isInRange) {
      return false;
    }
  }
  config.currentRetryAttempt = config.currentRetryAttempt || 0;
  if (config.currentRetryAttempt >= config.retry) {
    return false;
  }
  return true;
};
const onError = (err) => {
  if (!err.config) {
    return Promise.reject(err);
  }
  const config = err.config.retryConfig || {};
  config.currentRetryAttempt = config.currentRetryAttempt || 0;
  config.retry = config.retry === void 0 || config.retry === null ? 3 : config.retry;
  config.retryDelay = config.retryDelay || 100;
  config.instance = config.instance || import_axios.default;
  config.httpMethodsToRetry = config.httpMethodsToRetry || [
    "GET",
    "HEAD",
    "PUT",
    "OPTIONS",
    "DELETE"
  ];
  config.noResponseRetries = config.noResponseRetries === void 0 || config.noResponseRetries === null ? 2 : config.noResponseRetries;
  const retryRanges = [
    // https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    // 1xx - Retry (Informational, request still processing)
    // 2xx - Do not retry (Success)
    // 3xx - Do not retry (Redirect)
    // 4xx - Do not retry (Client errors)
    // 429 - Retry ("Too Many Requests")
    // 5xx - Retry (Server errors)
    [100, 199],
    [429, 429],
    [500, 599]
  ];
  config.statusCodesToRetry = config.statusCodesToRetry || retryRanges;
  err.config.retryConfig = config;
  const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
  if (!shouldRetryFn(err)) {
    return Promise.reject(err);
  }
  const onBackoffPromise = new Promise((resolve) => {
    const delay = (2 ** config.currentRetryAttempt - 1) / 2 * 1e3;
    err.config.retryConfig.currentRetryAttempt += 1;
    setTimeout(resolve, delay);
  });
  const onRetryAttemptPromise = config.onRetryAttempt ? Promise.resolve(config.onRetryAttempt(err)) : Promise.resolve();
  return Promise.resolve().then(() => onBackoffPromise).then(() => onRetryAttemptPromise).then(() => config.instance.request(err.config));
};
const attach = (instance) => {
  instance = instance || import_axios.default;
  return instance?.interceptors?.response ? instance.interceptors.response.use(onFulfilled, onError) : null;
};
const getConfig = (err) => {
  if (err && err.config) {
    return err.config.retryConfig;
  }
  return null;
};
