"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_jre_from_download = require('./from-download.js');
const require_external_tools_jre_from_java_home = require('./from-java-home.js');
const require_external_tools_jre_from_path = require('./from-path.js');
const require_external_tools_jre_from_vfs = require('./from-vfs.js');

//#region src/external-tools/jre/resolve.ts
/**
* @file `resolveJre()` — the JRE resolution entry point. Tries each source in
*   order:
*
*   1. VFS — smol binary's embedded JRE (zero network, fast)
*   2. JAVA_HOME — user-pinned via env var
*   3. PATH — `java` (or `java.exe`) on the system PATH
*   4. download — Adoptium fetch + extract (only when `downloadIfMissing` is
*      passed) Returns `undefined` if all of the enabled sources miss. The
*      caller decides what to do then — typically prompt the user, surface an
*      install instruction, or fail with an actionable error. 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 even after the first call's
*      "all local tiers missed → undefined" memoized. Test-only escape hatch:
*      `resetJreResolution()` clears the cache so tests can exercise the
*      resolver fresh.
*/
const resolutionCache = new require_primordials_map_set.MapCtor();
function cacheKey(opts) {
	if (!opts?.downloadIfMissing) return "local-only";
	const { cacheDir, integrity, platformArch, version } = opts.downloadIfMissing;
	return `dl:${version}:${platformArch}:${typeof integrity === "string" ? integrity : integrity ? `${integrity.type}:${integrity.value}` : ""}:${cacheDir ?? ""}`;
}
async function doResolveJre(opts) {
	const fromVfs = await require_external_tools_jre_from_vfs.jreFromVfs();
	/* c8 ignore start - smol Node binary only. */
	if (fromVfs) return fromVfs;
	/* c8 ignore stop */
	const fromJavaHome = require_external_tools_jre_from_java_home.jreFromJavaHome();
	if (fromJavaHome) return fromJavaHome;
	const fromPath = await require_external_tools_jre_from_path.jreFromPath();
	if (fromPath) return fromPath;
	if (opts?.downloadIfMissing) return require_external_tools_jre_from_download.jreFromDownload(opts.downloadIfMissing);
}
/* c8 ignore start - test-only escape hatch. */
function resetJreResolution() {
	resolutionCache.clear();
}
/* c8 ignore stop */
function resolveJre(opts) {
	const key = cacheKey(opts);
	let cached = resolutionCache.get(key);
	if (!cached) {
		cached = doResolveJre(opts);
		resolutionCache.set(key, cached);
	}
	return cached;
}

//#endregion
exports.cacheKey = cacheKey;
exports.doResolveJre = doResolveJre;
exports.resetJreResolution = resetJreResolution;
exports.resolveJre = resolveJre;