"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_opengrep_from_download = require('./from-download.js');
const require_external_tools_opengrep_from_path = require('./from-path.js');
const require_external_tools_opengrep_from_vfs = require('./from-vfs.js');

//#region src/external-tools/opengrep/resolve.ts
/**
* @file `resolveOpengrep()` — OpenGrep resolution entry point. Tries each
*   source in order:
*
*   1. VFS — smol binary's embedded OpenGrep (if packed)
*   2. PATH — `opengrep` on the system PATH
*   3. download — upstream GitHub release asset (only when `downloadIfMissing` is
*      passed) Returns `undefined` if all of the enabled sources miss. 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 doResolveOpengrep(opts) {
	const fromVfs = await require_external_tools_opengrep_from_vfs.opengrepFromVfs();
	/* c8 ignore start - smol Node binary only. */
	if (fromVfs) return fromVfs;
	/* c8 ignore stop */
	const fromPath = await require_external_tools_opengrep_from_path.opengrepFromPath();
	if (fromPath) return fromPath;
	if (opts?.downloadIfMissing) return require_external_tools_opengrep_from_download.opengrepFromDownload(opts.downloadIfMissing);
}
/* c8 ignore start - test-only escape hatch. */
function resetOpengrepResolution() {
	resolutionCache.clear();
}
/* c8 ignore stop */
function resolveOpengrep(opts) {
	const key = cacheKey(opts);
	let cached = resolutionCache.get(key);
	if (!cached) {
		cached = doResolveOpengrep(opts);
		resolutionCache.set(key, cached);
	}
	return cached;
}

//#endregion
exports.cacheKey = cacheKey;
exports.doResolveOpengrep = doResolveOpengrep;
exports.resetOpengrepResolution = resetOpengrepResolution;
exports.resolveOpengrep = resolveOpengrep;