"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_string = require('../primordials/string.js');
const require_primordials_regexp = require('../primordials/regexp.js');
const require_paths_socket = require('../paths/socket.js');
const require_cacache__internal = require('./_internal.js');

//#region src/cacache/clear.ts
/**
* @file Bulk-clear entries from the Socket shared cacache — `clear()` plus its
*   wildcard helper `createPatternMatcher`. The helper is exported because
*   callers occasionally compose their own filtering pipelines.
*/
/**
* Clear entries from the Socket shared cache.
*
* Supports wildcard patterns (*) in prefix for flexible matching. For simple
* prefixes without wildcards, uses efficient streaming. For wildcard patterns,
* iterates and matches each entry.
*
* @example
*   // Clear all entries
*   await clear()
*
* @example
*   // Clear entries with simple prefix
*   const removed = await clear({ prefix: 'socket-sdk:scans' })
*   console.log(`Removed ${removed} scan cache entries`)
*
* @example
*   // Clear entries with wildcard pattern
*   await clear({ prefix: 'socket-sdk:scans:abc*' })
*   await clear({ prefix: 'socket-sdk:npm/lodash/*' })
*
* @param options - Optional configuration for selective clearing.
* @param options.prefix - Prefix or pattern to match (supports * wildcards)
*
* @returns Number of entries removed (only when prefix is specified)
*/
async function clear(options) {
	const opts = {
		__proto__: null,
		...options
	};
	const cacache = require_cacache__internal.getCacache();
	const cacheDir = require_paths_socket.getSocketCacacheDir();
	if (!opts.prefix) try {
		/* c8 ignore next - External cacache call */
		await cacache.rm.all(cacheDir);
		return;
	} catch (e) {
		if (e?.code !== "ENOTEMPTY") throw e;
		return;
	}
	let removed = 0;
	const matches = createPatternMatcher(opts.prefix);
	/* c8 ignore next - External cacache call */
	const stream = cacache.ls.stream(cacheDir);
	for await (const entry of stream) if (matches(entry.key)) try {
		/* c8 ignore next - External cacache call */
		await cacache.rm.entry(cacheDir, entry.key);
		removed++;
	} catch {}
	return removed;
}
/**
* Build a key→boolean matcher for `pattern`. For non-wildcard patterns this
* returns a prefix-startsWith predicate (no regex allocation); for wildcard
* patterns it compiles the regex _once_ and closes over it so the caller can
* apply the same matcher across N keys in O(1)-per-key.
*
* Anchors both ends — `foo*bar` matches exactly `foo<anything>bar`, not
* `foo<anything>bar<more>`.
*/
function createPatternMatcher(pattern) {
	if (!pattern.includes("*")) return (key) => require_primordials_string.StringPrototypeStartsWith(key, pattern);
	const regex = new require_primordials_regexp.RegExpCtor(`^${require_primordials_string.StringPrototypeReplaceAll(require_primordials_string.StringPrototypeReplaceAll(pattern, /[.+?^${}()|[\]\\]/g, "\\$&"), "*", ".*")}$`);
	return (key) => require_primordials_regexp.RegExpPrototypeTest(regex, key);
}

//#endregion
exports.clear = clear;
exports.createPatternMatcher = createPatternMatcher;