"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_janus_from_download = require('./from-download.js');
const require_external_tools_janus_from_path = require('./from-path.js');
const require_external_tools_janus_from_vfs = require('./from-vfs.js');

//#region src/external-tools/janus/resolve.ts
/**
* @file `resolveJanus()` — janus resolution entry point. Tries each source in
*   order:
*
*   1. VFS — smol binary's embedded janus (if packed)
*   2. PATH — `janus` on the system PATH
*   3. download — upstream GitHub release tar.gz (only when `downloadIfMissing` is
*      passed) Returns `undefined` if all of the enabled sources miss —
*      including when `downloadIfMissing` is set but the requested
*      platform-arch isn't shipped by upstream janus (currently darwin-arm64
*      only). Memoized per option-shape.
*/
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 doResolveJanus(opts) {
	const fromVfs = await require_external_tools_janus_from_vfs.janusFromVfs();
	/* c8 ignore start - smol Node binary only. */
	if (fromVfs) return fromVfs;
	/* c8 ignore stop */
	const fromPath = await require_external_tools_janus_from_path.janusFromPath();
	if (fromPath) return fromPath;
	if (opts?.downloadIfMissing) return require_external_tools_janus_from_download.janusFromDownload(opts.downloadIfMissing);
}
/* c8 ignore start - test-only escape hatch. */
function resetJanusResolution() {
	resolutionCache.clear();
}
/* c8 ignore stop */
function resolveJanus(opts) {
	const key = cacheKey(opts);
	let cached = resolutionCache.get(key);
	if (!cached) {
		cached = doResolveJanus(opts);
		resolutionCache.set(key, cached);
	}
	return cached;
}

//#endregion
exports.cacheKey = cacheKey;
exports.doResolveJanus = doResolveJanus;
exports.resetJanusResolution = resetJanusResolution;
exports.resolveJanus = resolveJanus;