"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 refs_graphql_exports = {};
__export(refs_graphql_exports, {
  fetchRefShaViaGraphQL: () => fetchRefShaViaGraphQL
});
module.exports = __toCommonJS(refs_graphql_exports);
var import_request = require("../http-request/request");
var import_json = require("../primordials/json");
var import_token = require("./token");
var import_constants = require("./constants");
async function fetchRefShaViaGraphQL(owner, repo, ref, options) {
  const token = options.token || (0, import_token.getGitHubToken)();
  const headers = {
    Accept: "application/vnd.github.v3+json",
    "Content-Type": "application/json",
    "User-Agent": "socket-registry-github-client",
    ...options.headers
  };
  if (token) {
    headers["Authorization"] = `Bearer ${token}`;
  }
  const query = `query($owner: String!, $repo: String!, $tag: String!, $branch: String!, $oid: GitObjectID!) {
    repository(owner: $owner, name: $repo) {
      tagRef: ref(qualifiedName: $tag) {
        target {
          __typename
          ... on Tag { target { oid } }
          ... on Commit { oid }
        }
      }
      branchRef: ref(qualifiedName: $branch) {
        target { oid }
      }
      commit: object(oid: $oid) {
        __typename
        ... on Commit { oid }
      }
    }
  }`;
  const looksLikeSha = /^[a-f0-9]{40}$/i.test(ref);
  const oidArg = looksLikeSha ? ref : "0000000000000000000000000000000000000000";
  const response = await (0, import_request.httpRequest)(import_constants.GITHUB_GRAPHQL_URL, {
    body: (0, import_json.JSONStringify)({
      query,
      variables: {
        branch: `refs/heads/${ref}`,
        oid: oidArg,
        owner,
        repo,
        tag: `refs/tags/${ref}`
      }
    }),
    headers,
    method: "POST"
  });
  if (!response.ok || response.body.byteLength === 0) {
    return void 0;
  }
  let parsed;
  try {
    parsed = (0, import_json.JSONParse)(response.body.toString("utf8"));
  } catch {
    return void 0;
  }
  const repoData = parsed.data?.repository;
  if (!repoData) {
    return void 0;
  }
  const tagTarget = repoData.tagRef?.target;
  if (tagTarget) {
    if (tagTarget.__typename === "Tag") {
      return tagTarget.target?.oid ?? void 0;
    }
    if (tagTarget.__typename === "Commit") {
      return tagTarget.oid ?? void 0;
    }
  }
  const branchOid = repoData.branchRef?.target?.oid;
  if (branchOid) {
    return branchOid;
  }
  if (repoData.commit?.__typename === "Commit" && repoData.commit.oid) {
    return repoData.commit.oid;
  }
  return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fetchRefShaViaGraphQL
});
