"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../../primordials/error.js');
const require_primordials_regexp = require('../../primordials/regexp.js');
const require_smol_manifest = require('../../smol/manifest.js');

//#region src/eco/manifest/find-packages.ts
/**
* @file `findPackages(lockfile, pattern)` — returns all `PackageRef`s whose
*   name matches the regex/string pattern. Pattern length is capped at 256
*   chars to bound ReDoS amplification against large lockfiles. Both string and
*   RegExp inputs are hardened via `hardenRegExp` (where available) — matches
*   socket-btm's smol behavior. Throws:
*
*   - `TypeError` — pattern is not a string or RegExp
*   - `RangeError` — pattern exceeds FIND_PACKAGES_PATTERN_MAX_LEN
*/
const FIND_PACKAGES_PATTERN_MAX_LEN = 256;
function jsFindPackages(lockfile, pattern) {
	let regex;
	if (pattern instanceof require_primordials_regexp.RegExpCtor) {
		if (pattern.source.length > 256) throw new require_primordials_error.RangeErrorCtor(`pattern exceeds maximum length (${256})`);
		regex = pattern;
	} else {
		if (typeof pattern !== "string") throw new require_primordials_error.TypeErrorCtor("pattern must be a string or RegExp");
		if (pattern.length > 256) throw new require_primordials_error.RangeErrorCtor(`pattern exceeds maximum length (${256})`);
		regex = new require_primordials_regexp.RegExpCtor(pattern);
	}
	const result = [];
	const pkgs = lockfile.packages;
	for (let i = 0, { length } = pkgs; i < length; i++) {
		const pkg = pkgs[i];
		if (require_primordials_regexp.RegExpPrototypeTest(regex, pkg.name)) result[result.length] = pkg;
	}
	return result;
}
const _smol = /* @__PURE__ */ require_smol_manifest.getSmolManifest();
const findPackages = _smol ? _smol.findPackages : jsFindPackages;

//#endregion
exports.FIND_PACKAGES_PATTERN_MAX_LEN = FIND_PACKAGES_PATTERN_MAX_LEN;
exports.findPackages = findPackages;
exports.jsFindPackages = jsFindPackages;