"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_errors_message = require('../errors/message.js');
const require_github_constants = require('./constants.js');
const require_github_errors = require('./errors.js');
const require_github_fetch = require('./fetch.js');
const require_github_refs_graphql = require('./refs-graphql.js');

//#region src/github/refs-rest.ts
/**
* @file Resolve a GitHub git ref via REST tier-cascade. Split out of
*   `github/refs.ts` for size hygiene. Walks tag → branch → commit endpoints in
*   sequence; the first 200 OK wins. If any tier hits the documented
*   200-OK-empty-body incident shape, falls back to the GraphQL transport in
*   `./refs-graphql`.
*/
/**
* Fetch the SHA for a git ref from GitHub API. Internal helper that implements
* the multi-strategy ref resolution logic. Tries tags, branches, and direct
* commit lookups in sequence.
*
* @param owner - Repository owner.
* @param repo - Repository name.
* @param ref - Git reference to resolve.
* @param options - Resolution options with authentication token.
*
* @returns The full commit SHA
*
* @throws {Error} When ref cannot be resolved after all strategies fail
*/
async function fetchRefSha(owner, repo, ref, options) {
	const fetchOptions = { token: options.token };
	let sawEmptyBody = false;
	const note404 = (e) => {
		if (e instanceof require_github_errors.GitHubEmptyBodyError) sawEmptyBody = true;
		return e;
	};
	try {
		const tagData = await require_github_fetch.fetchGitHub(`${require_github_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/git/refs/tags/${ref}`, fetchOptions);
		if (tagData.object.type === "tag") return (await require_github_fetch.fetchGitHub(tagData.object.url, fetchOptions)).object.sha;
		return tagData.object.sha;
	} catch (e) {
		note404(e);
		try {
			return (await require_github_fetch.fetchGitHub(`${require_github_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/git/refs/heads/${ref}`, fetchOptions)).object.sha;
		} catch (e2) {
			note404(e2);
			try {
				return (await require_github_fetch.fetchGitHub(`${require_github_constants.GITHUB_API_BASE_URL}/repos/${owner}/${repo}/commits/${ref}`, fetchOptions)).sha;
			} catch (e3) {
				note404(e3);
				if (sawEmptyBody) {
					let graphqlSha;
					let graphqlErr;
					try {
						graphqlSha = await require_github_refs_graphql.fetchRefShaViaGraphQL(owner, repo, ref, fetchOptions);
					} catch (cause) {
						graphqlErr = cause;
					}
					if (graphqlSha) return graphqlSha;
					/* c8 ignore start */
					if (graphqlErr !== void 0) throw new require_primordials_error.ErrorCtor(`Failed to resolve ref "${ref}" for ${owner}/${repo}: both REST and GraphQL backends degraded`, { cause: graphqlErr });
				}
				throw new require_primordials_error.ErrorCtor(`Failed to resolve ref "${ref}" for ${owner}/${repo}: ${require_errors_message.errorMessage(e3)}`);
			}
		}
	}
}

//#endregion
exports.fetchRefSha = fetchRefSha;