"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 request_exports = {};
__export(request_exports, {
  httpRequest: () => httpRequest,
  httpRequestAttempt: () => import_request_attempt2.httpRequestAttempt,
  readIncomingResponse: () => import_response_reader.readIncomingResponse
});
module.exports = __toCommonJS(request_exports);
var import_promises = require("node:timers/promises");
var import_error = require("../primordials/error");
var import_math = require("../primordials/math");
var import_number = require("../primordials/number");
var import_request_attempt = require("./request-attempt");
var import_response_types = require("./response-types");
var import_request_attempt2 = require("./request-attempt");
var import_response_reader = require("./response-reader");
async function httpRequest(url, options) {
  const {
    body,
    ca,
    followRedirects = true,
    headers = {},
    hooks,
    maxRedirects = 5,
    maxResponseSize,
    method = "GET",
    onRetry,
    retries = 0,
    retryDelay = 1e3,
    stream = false,
    throwOnError = false,
    timeout = 3e4
  } = { __proto__: null, ...options };
  const isStreamBody = body !== void 0 && typeof body === "object" && typeof body.pipe === "function";
  if (isStreamBody && retries > 0) {
    throw new import_error.ErrorCtor(
      "Streaming body (Readable/FormData) cannot be used with retries. Streams are consumed on first attempt and cannot be replayed. Set retries: 0 or buffer the body as a string/Buffer."
    );
  }
  const attemptOpts = {
    body,
    ca,
    // Disable redirect following for stream bodies — the stream is consumed
    // on the first request and cannot be re-piped to the redirect target.
    followRedirects: isStreamBody ? false : followRedirects,
    headers,
    hooks,
    maxRedirects,
    maxResponseSize,
    method,
    stream,
    timeout
  };
  let lastError;
  for (let attempt = 0; attempt <= retries; attempt++) {
    try {
      const response = await (0, import_request_attempt.httpRequestAttempt)(url, attemptOpts);
      if (throwOnError && !response.ok) {
        throw new import_response_types.HttpResponseError(response);
      }
      return response;
    } catch (e) {
      lastError = e;
      if (attempt === retries) {
        break;
      }
      const delayMs = retryDelay * 2 ** attempt;
      if (onRetry) {
        const retryResult = onRetry(attempt + 1, e, delayMs);
        if (retryResult === false) {
          break;
        }
        const actualDelay = typeof retryResult === "number" && !(0, import_number.NumberIsNaN)(retryResult) ? (0, import_math.MathMax)(0, retryResult) : delayMs;
        await (0, import_promises.setTimeout)(actualDelay);
      } else {
        await (0, import_promises.setTimeout)(delayMs);
      }
    }
  }
  throw lastError || new import_error.ErrorCtor("Request failed after retries");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  httpRequest,
  httpRequestAttempt,
  readIncomingResponse
});
