"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 fetch_exports = {};
__export(fetch_exports, {
  fetchGitHub: () => fetchGitHub,
  getGhsaUrl: () => getGhsaUrl
});
module.exports = __toCommonJS(fetch_exports);
var import_message = require("../errors/message");
var import_request = require("../http-request/request");
var import_date = require("../primordials/date");
var import_error = require("../primordials/error");
var import_json = require("../primordials/json");
var import_token = require("./token");
var import_errors = require("./errors");
async function fetchGitHub(url, options) {
  const opts = { __proto__: null, ...options };
  const token = opts.token || (0, import_token.getGitHubToken)();
  const headers = {
    Accept: "application/vnd.github.v3+json",
    "User-Agent": "socket-registry-github-client",
    ...opts.headers
  };
  if (token) {
    headers["Authorization"] = `Bearer ${token}`;
  }
  const response = await (0, import_request.httpRequest)(url, { headers });
  if (!response.ok) {
    if (response.status === 403) {
      const rateLimit = response.headers["x-ratelimit-remaining"];
      const rateLimitStr = typeof rateLimit === "string" ? rateLimit : rateLimit?.[0];
      if (rateLimitStr === "0") {
        const resetTime = response.headers["x-ratelimit-reset"];
        const resetTimeStr = typeof resetTime === "string" ? resetTime : resetTime?.[0];
        const resetDate = resetTimeStr ? new import_date.DateCtor(Number(resetTimeStr) * 1e3) : void 0;
        const error = new import_error.ErrorCtor(
          `GitHub API rate limit exceeded${resetDate ? `. Resets at ${resetDate.toLocaleString()}` : ""}. Use GITHUB_TOKEN environment variable to increase rate limit.`
        );
        error.status = 403;
        error.resetTime = resetDate;
        throw error;
      }
    }
    throw new import_error.ErrorCtor(
      `GitHub API error ${response.status}: ${response.statusText}`
    );
  }
  if (response.body.byteLength === 0) {
    throw new import_errors.GitHubEmptyBodyError(url);
  }
  try {
    return (0, import_json.JSONParse)(response.body.toString("utf8"));
  } catch (e) {
    throw new import_error.ErrorCtor(
      `Failed to parse GitHub API response: ${(0, import_message.errorMessage)(e)}
URL: ${url}
Response may be malformed or incomplete.`,
      { cause: e }
    );
  }
}
function getGhsaUrl(ghsaId) {
  return `https://github.com/advisories/${ghsaId}`;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fetchGitHub,
  getGhsaUrl
});
