"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_map_set = require('../../primordials/map-set.js');
const require_external_tools_bazel_from_download = require('./from-download.js');
const require_external_tools_bazel_from_path = require('./from-path.js');

//#region src/external-tools/bazel/resolve.ts
/**
* @file `resolveBazel()` — Bazel resolution entry point. Tries each source in
*   order:
*
*   1. PATH — `bazelisk` (preferred) or `bazel` on the system PATH
*   2. download — upstream GitHub release binary (only when `downloadIfMissing` is
*      passed) No VFS tier: Bazel's version comes from the project's
*      `.bazelversion`, not from a global pin, so a smol-bundled Bazel would
*      always be the wrong version for any project that pinned a different one.
*      Every Bazel use must go through the project-specific download path.
*      Returns `undefined` if all of the enabled sources miss. Memoized per
*      option-shape: calls with identical options return the same cached
*      promise. Calling without `downloadIfMissing` and then with
*      `downloadIfMissing` produces two distinct cache entries so the second
*      call can fall through to the download tier.
*/
const resolutionCache = new require_primordials_map_set.MapCtor();
function cacheKey(opts) {
	if (!opts?.downloadIfMissing) return "local-only";
	const { integrity, platformArch, version } = opts.downloadIfMissing;
	return `dl:${version}:${platformArch}:${typeof integrity === "string" ? integrity : integrity ? `${integrity.type}:${integrity.value}` : ""}`;
}
async function doResolveBazel(opts) {
	const fromPath = await require_external_tools_bazel_from_path.bazelFromPath();
	if (fromPath) return fromPath;
	if (opts?.downloadIfMissing) return require_external_tools_bazel_from_download.bazelFromDownload(opts.downloadIfMissing);
}
/* c8 ignore start - test-only escape hatch. */
function resetBazelResolution() {
	resolutionCache.clear();
}
/* c8 ignore stop */
function resolveBazel(opts) {
	const key = cacheKey(opts);
	let cached = resolutionCache.get(key);
	if (!cached) {
		cached = doResolveBazel(opts);
		resolutionCache.set(key, cached);
	}
	return cached;
}

//#endregion
exports.cacheKey = cacheKey;
exports.doResolveBazel = doResolveBazel;
exports.resetBazelResolution = resetBazelResolution;
exports.resolveBazel = resolveBazel;