"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_array = require('../primordials/array.js');
const require_primordials_object = require('../primordials/object.js');
const require_primordials_json = require('../primordials/json.js');
const require_globs__internal = require('./_internal.js');

//#region src/globs/matcher.ts
/**
* @file `getGlobMatcher` — picomatch-backed sync predicate with an LRU-memoized
*   matcher cache. `getMatchesGlob` exposes Node 22+'s native
*   `path.matchesGlob` for the rare case where the caller wants strict
*   (`nocase: false`, `dot: false`) matching.
*/
let _matchesGlob;
let _matchesGlobProbed = false;
/**
* Return a glob-matcher function, memoized by pattern + options.
*
* The returned function is a fast synchronous predicate built on picomatch.
* Results are memoized — calling `getGlobMatcher(['*.ts'])` a thousand times in
* a loop returns the same compiled matcher each time, so callers do not need to
* hoist it themselves.
*
* The cache is LRU with a cap of 100 entries. Cache keys fold together the
* (sorted) pattern list and (sorted) option set, so arguments that differ only
* in ordering share a matcher.
*
* Default options: `dot: true`, `nocase: true`. Patterns starting with `!`
* become ignore patterns.
*
* @example
*   ;```typescript
*   const isMatch = getGlobMatcher('*.ts')
*   isMatch('index.ts') // true
*   isMatch('index.js') // false
*
*   const isSource = getGlobMatcher(['src/**', '!**\/*.test.ts'])
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function getGlobMatcher(glob, options) {
	const patterns = require_primordials_array.ArrayIsArray(glob) ? glob : [glob];
	const sortedPatterns = [...patterns].sort();
	const sortedOptions = options ? require_primordials_object.ObjectKeys(options).sort().map((k) => {
		const value = options[k];
		return `${k}:${require_primordials_json.JSONStringify(require_primordials_array.ArrayIsArray(value) ? [...value].sort() : value)}`;
	}).join(",") : "";
	const key = `${sortedPatterns.join("|")}:${sortedOptions}`;
	const existing = require_globs__internal.matcherCache.get(key);
	if (existing) {
		require_globs__internal.matcherCache.delete(key);
		require_globs__internal.matcherCache.set(key, existing);
		return existing;
	}
	/* c8 ignore start */
	if (require_globs__internal.matcherCache.size >= 100) {
		const oldest = require_globs__internal.matcherCache.keys().next().value;
		if (oldest !== void 0) require_globs__internal.matcherCache.delete(oldest);
	}
	/* c8 ignore stop */
	let matcher;
	/* c8 ignore start */
	if (patterns.length === 1 && !require_primordials_string.StringPrototypeStartsWith(patterns[0], "!") && options !== void 0 && options.nocase === false && options.dot === false && (options.ignore === void 0 || options.ignore.length === 0)) {
		const matchesGlob = /* @__PURE__ */ getMatchesGlob();
		if (matchesGlob !== void 0) {
			const pattern = patterns[0];
			matcher = (p) => matchesGlob(p, pattern);
		}
	}
	/* c8 ignore stop */
	if (matcher === void 0) {
		const positivePatterns = patterns.filter((p) => !require_primordials_string.StringPrototypeStartsWith(p, "!"));
		const negativePatterns = patterns.filter((p) => require_primordials_string.StringPrototypeStartsWith(p, "!")).map((p) => p.slice(1));
		const matchOptions = {
			dot: true,
			nocase: true,
			...options,
			...negativePatterns.length > 0 ? { ignore: negativePatterns } : {}
		};
		matcher = (/* @__PURE__ */ require_globs__internal.getPicomatch())(positivePatterns.length > 0 ? positivePatterns : patterns, matchOptions);
	}
	require_globs__internal.matcherCache.set(key, matcher);
	return matcher;
}
/**
* Resolve `path.matchesGlob` (or `undefined` if the runtime predates it).
* Probes once and caches the result for every subsequent call.
*
* Used by `getGlobMatcher`'s narrow fast-path — see the conditions spelled out
* at the call site. Exported for unit tests.
*
* @internal
*/
/*@__NO_SIDE_EFFECTS__*/
function getMatchesGlob() {
	if (!_matchesGlobProbed) {
		const fn = (/* @__PURE__ */ require("node:path")).matchesGlob;
		/* c8 ignore start */
		if (typeof fn === "function") _matchesGlob = fn;
		/* c8 ignore stop */
		_matchesGlobProbed = true;
	}
	return _matchesGlob;
}

//#endregion
exports.getGlobMatcher = getGlobMatcher;
exports.getMatchesGlob = getMatchesGlob;