"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_rest_exports = {};
__export(refs_rest_exports, {
  fetchRefSha: () => fetchRefSha
});
module.exports = __toCommonJS(refs_rest_exports);
var import_message = require("../errors/message");
var import_error = require("../primordials/error");
var import_fetch = require("./fetch");
var import_refs_graphql = require("./refs-graphql");
var import_constants = require("./constants");
var import_errors = require("./errors");
async function fetchRefSha(owner, repo, ref, options) {
  const fetchOptions = {
    token: options.token
  };
  let sawEmptyBody = false;
  const note404 = (e) => {
    if (e instanceof import_errors.GitHubEmptyBodyError) {
      sawEmptyBody = true;
    }
    return e;
  };
  try {
    const tagUrl = `${import_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/git/refs/tags/${ref}`;
    const tagData = await (0, import_fetch.fetchGitHub)(tagUrl, fetchOptions);
    if (tagData.object.type === "tag") {
      const tagObject = await (0, import_fetch.fetchGitHub)(
        tagData.object.url,
        fetchOptions
      );
      return tagObject.object.sha;
    }
    return tagData.object.sha;
  } catch (e) {
    note404(e);
    try {
      const branchUrl = `${import_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/git/refs/heads/${ref}`;
      const branchData = await (0, import_fetch.fetchGitHub)(branchUrl, fetchOptions);
      return branchData.object.sha;
    } catch (e2) {
      note404(e2);
      try {
        const commitUrl = `${import_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/commits/${ref}`;
        const commitData = await (0, import_fetch.fetchGitHub)(
          commitUrl,
          fetchOptions
        );
        return commitData.sha;
      } catch (e3) {
        note404(e3);
        if (sawEmptyBody) {
          let graphqlSha;
          let graphqlErr;
          try {
            graphqlSha = await (0, import_refs_graphql.fetchRefShaViaGraphQL)(
              owner,
              repo,
              ref,
              fetchOptions
            );
          } catch (cause) {
            graphqlErr = cause;
          }
          if (graphqlSha) {
            return graphqlSha;
          }
          if (graphqlErr !== void 0) {
            throw new import_error.ErrorCtor(
              `Failed to resolve ref "${ref}" for ${owner}/${repo}: both REST and GraphQL backends degraded`,
              { cause: graphqlErr }
            );
          }
        }
        throw new import_error.ErrorCtor(
          `Failed to resolve ref "${ref}" for ${owner}/${repo}: ${(0, import_message.errorMessage)(e3)}`
        );
      }
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fetchRefSha
});
