"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_math = require('../../../../primordials/math.js');
const require_primordials_array = require('../../../../primordials/array.js');
const require_primordials_object = require('../../../../primordials/object.js');
const require_smol_manifest = require('../../../../smol/manifest.js');
const require_eco_npm_yarnpkg_yarn_parse_yarn_descriptor = require('./parse-yarn-descriptor.js');

//#region src/eco/npm/yarnpkg/yarn/parse-lockfile.ts
/**
* @file `parseYarnLock(content)` — parses a `yarn.lock` (Classic v1 or Berry
*   v6) into a `ParsedLockfile`. On socket-btm's smol Node binary this routes
*   to `node:smol-manifest`'s native `parseLockfile(content, 'npm', 'yarn')`;
*   on stock Node it runs the line-scanning JS impl below. Both Classic + Berry
*   share the same outer shape (top-level block with `name@spec:` header +
*   indented properties). Berry is detected via the presence of `__metadata:`
*   and additionally honors `linkType: soft` (workspace links are skipped) and
*   `dependenciesMeta:` blocks (which can mark deps optional). The parser is
*   forgiving — unknown lines are ignored, missing versions skip the entry. It
*   never throws. Source material (in lock-step order, newest → oldest):
*
*   1. **C++ native parser** in socket-btm/node-smol-builder:
*      additions/source-patched/src/socketsecurity/manifest/parser_yarn.cc Same
*      algorithm, same fixes — keep the two in lock-step.
*   2. **socket-sdxgen** — algorithm oracle:
*      socket-sdxgen/src/parsers/yarn-classic/yarn-lock-v1.mts
*      socket-sdxgen/src/parsers/yarn-berry/yarn-lock-v2.mts sdxgen uses
*      `@yarnpkg/parsers.parseSyml`; this TS port + the C++ port both use a
*      line walker (yarn's syml-as-it-uses-it is a strict subset).
*   3. **cdxgen** (pinned v11.11.0):
*      https://github.com/CycloneDX/cdxgen/blob/v11.11.0/lib/parsers/js.js
*      (parseYarnLock)
*   4. **yarn format docs**: classic (v1):
*      https://github.com/yarnpkg/yarn/blob/master/src/lockfile/parse.js berry
*      (v2+): https://yarnpkg.com/configuration/yarnrc#lockfileVersion
*      protocols: https://yarnpkg.com/protocol/ Bug fixes implemented here (and
*      in the native parser):
*
*   - Fix 4 `dependenciesMeta` block consume-only. Earlier impls were doing two
*     wrong things at once: (a) synthesizing phantom PackageRefs for children,
*     and (b) flipping the PARENT's `isOptional` based on any child's
*     `optional: true` flag — inverted semantics. The fix consumes the block
*     for position only; metadata flags refer to a child's relationship from
*     the parent's view, never the parent itself. Regression fixtures live
*     under socket-btm's test/fixtures/ sdxgen-bug-regressions/ — every shipped
*     fix has a matching fixture directory there.
*/
function addToYarnIndex(packageIndex, name, idx) {
	const existing = packageIndex[name];
	if (existing === void 0) packageIndex[name] = idx;
	else if (typeof existing === "number") packageIndex[name] = [existing, idx];
	else require_primordials_array.ArrayPrototypePush(existing, idx);
}
function consumeDependenciesMeta(content, startPos, _entry) {
	let pos = startPos;
	while (pos < content.length) {
		const eol = require_primordials_string.StringPrototypeIndexOf(content, "\n", pos);
		const end = eol === -1 ? content.length : eol;
		const line = require_primordials_string.StringPrototypeSlice(content, pos, end);
		if (line.length < 4 || line[0] !== " " || line[1] !== " " || line[2] !== " " || line[3] !== " ") break;
		pos = end + 1;
	}
	return pos;
}
function consumeDependencyList(content, startPos, deps) {
	let pos = startPos;
	while (pos < content.length) {
		const eol = require_primordials_string.StringPrototypeIndexOf(content, "\n", pos);
		const end = eol === -1 ? content.length : eol;
		const line = require_primordials_string.StringPrototypeSlice(content, pos, end);
		if (line.length < 4 || line[0] !== " " || line[1] !== " " || line[2] !== " " || line[3] !== " ") break;
		const depLine = require_primordials_string.StringPrototypeTrim(line);
		const colonIdx = require_primordials_string.StringPrototypeIndexOf(depLine, ":");
		const spaceIdx = require_primordials_string.StringPrototypeIndexOf(depLine, " ");
		const sepIdx = colonIdx === -1 ? spaceIdx : spaceIdx === -1 ? colonIdx : require_primordials_math.MathMin(colonIdx, spaceIdx);
		if (sepIdx > 0) require_primordials_array.ArrayPrototypePush(deps, stripQuotes(require_primordials_string.StringPrototypeSlice(depLine, 0, sepIdx)));
		pos = end + 1;
	}
	return pos;
}
function consumeEntryProperties(content, startPos, entry) {
	let pos = startPos;
	while (pos < content.length) {
		const eol = require_primordials_string.StringPrototypeIndexOf(content, "\n", pos);
		const end = eol === -1 ? content.length : eol;
		const line = require_primordials_string.StringPrototypeSlice(content, pos, end);
		if (line.length === 0 || line[0] !== " " && line[0] !== "	") break;
		const propLine = require_primordials_string.StringPrototypeTrim(line);
		pos = end + 1;
		if (require_primordials_string.StringPrototypeIndexOf(propLine, "version ") === 0 || require_primordials_string.StringPrototypeIndexOf(propLine, "version:") === 0) entry.version = stripQuotes(valueAfterKey(propLine, 8));
		else if (require_primordials_string.StringPrototypeIndexOf(propLine, "resolved ") === 0 || require_primordials_string.StringPrototypeIndexOf(propLine, "resolved:") === 0) entry.resolved = stripQuotes(valueAfterKey(propLine, 9));
		else if (require_primordials_string.StringPrototypeIndexOf(propLine, "integrity ") === 0 || require_primordials_string.StringPrototypeIndexOf(propLine, "integrity:") === 0) entry.integrity = valueAfterKey(propLine, 10);
		else if (require_primordials_string.StringPrototypeIndexOf(propLine, "checksum ") === 0 || require_primordials_string.StringPrototypeIndexOf(propLine, "checksum:") === 0) entry.checksum = valueAfterKey(propLine, 9);
		else if (require_primordials_string.StringPrototypeIndexOf(propLine, "linkType") === 0) {
			const colonIdx = require_primordials_string.StringPrototypeIndexOf(propLine, ":");
			if (colonIdx > 0) entry.linkType = require_primordials_string.StringPrototypeTrim(require_primordials_string.StringPrototypeSlice(propLine, colonIdx + 1));
		} else if (require_primordials_string.StringPrototypeIndexOf(propLine, "resolution") === 0) {
			const colonIdx = require_primordials_string.StringPrototypeIndexOf(propLine, ":");
			if (colonIdx > 0) {
				const resValue = stripQuotes(require_primordials_string.StringPrototypeTrim(require_primordials_string.StringPrototypeSlice(propLine, colonIdx + 1)));
				if (require_primordials_string.StringPrototypeIndexOf(resValue, "http://") === 0 || require_primordials_string.StringPrototypeIndexOf(resValue, "https://") === 0) entry.resolved = resValue;
			}
		} else if (require_primordials_string.StringPrototypeIndexOf(propLine, "dependencies:") === 0) pos = consumeDependencyList(content, pos, entry.dependencies);
		else if (require_primordials_string.StringPrototypeIndexOf(propLine, "dependenciesMeta:") === 0) pos = consumeDependenciesMeta(content, pos, entry);
	}
	return pos;
}
function jsParseYarnLock(content) {
	const packages = [];
	const packageIndex = { __proto__: null };
	const isBerry = require_primordials_string.StringPrototypeIndexOf(content, "__metadata:") !== -1;
	let pos = 0;
	while (pos < content.length) {
		const eol = require_primordials_string.StringPrototypeIndexOf(content, "\n", pos);
		const end = eol === -1 ? content.length : eol;
		const line = require_primordials_string.StringPrototypeSlice(content, pos, end);
		pos = end + 1;
		if (!line || require_primordials_string.StringPrototypeTrim(line) === "" || line[0] === "#") continue;
		if (require_primordials_string.StringPrototypeTrim(line) === "__metadata:") {
			pos = skipIndentedBlock(content, pos);
			continue;
		}
		if (line[0] === " " || line[0] === "	") continue;
		const trimmed = require_primordials_string.StringPrototypeTrim(line);
		if (!require_primordials_string.StringPrototypeEndsWith(trimmed, ":")) continue;
		let header = stripQuotes(require_primordials_string.StringPrototypeTrim(require_primordials_string.StringPrototypeSlice(trimmed, 0, -1)));
		const commaIdx = require_primordials_string.StringPrototypeIndexOf(header, ",");
		if (commaIdx !== -1) header = require_primordials_string.StringPrototypeTrim(require_primordials_string.StringPrototypeSlice(header, 0, commaIdx));
		if (require_primordials_string.StringPrototypeIndexOf(header, "@workspace:") !== -1) {
			pos = skipIndentedBlock(content, pos);
			continue;
		}
		const entry = newEntry(require_eco_npm_yarnpkg_yarn_parse_yarn_descriptor.parseYarnDescriptor(header).name);
		pos = consumeEntryProperties(content, pos, entry);
		if (isBerry && entry.linkType === "soft") continue;
		if (entry.name && entry.version) {
			require_primordials_array.ArrayPrototypePush(packages, require_primordials_object.ObjectFreeze({
				__proto__: null,
				name: entry.name,
				version: entry.version,
				resolved: entry.resolved,
				integrity: entry.integrity || entry.checksum || void 0,
				ecosystem: "npm",
				depType: "prod",
				isDev: false,
				isOptional: entry.isOptional,
				isPeer: false,
				isBundled: false,
				vcsUrl: void 0,
				vcsCommit: void 0,
				dependencies: entry.dependencies
			}));
			addToYarnIndex(packageIndex, entry.name, packages.length - 1);
		}
	}
	return require_primordials_object.ObjectFreeze({
		__proto__: null,
		type: "lockfile",
		lockVersion: isBerry ? "berry" : "1",
		ecosystem: "npm",
		packages: require_primordials_object.ObjectFreeze(packages),
		_index: packageIndex
	});
}
function newEntry(name) {
	return {
		name,
		version: void 0,
		resolved: void 0,
		integrity: void 0,
		checksum: void 0,
		dependencies: [],
		isOptional: false,
		linkType: void 0
	};
}
function skipIndentedBlock(content, startPos) {
	let pos = startPos;
	while (pos < content.length) {
		const eol = require_primordials_string.StringPrototypeIndexOf(content, "\n", pos);
		const end = eol === -1 ? content.length : eol;
		const line = require_primordials_string.StringPrototypeSlice(content, pos, end);
		if (line.length === 0 || line[0] !== " " && line[0] !== "	") return pos;
		pos = end + 1;
	}
	return pos;
}
function stripQuotes(s) {
	if (s.length === 0) return s;
	let out = s;
	if (require_primordials_string.StringPrototypeCharCodeAt(out, 0) === 34) out = require_primordials_string.StringPrototypeSlice(out, 1);
	if (out.length > 0 && require_primordials_string.StringPrototypeCharCodeAt(out, out.length - 1) === 34) out = require_primordials_string.StringPrototypeSlice(out, 0, -1);
	return out;
}
function valueAfterKey(line, keyLen) {
	return require_primordials_string.StringPrototypeTrim(require_primordials_string.StringPrototypeSlice(line, keyLen));
}
const _smol = /* @__PURE__ */ require_smol_manifest.getSmolManifest();
const parseYarnLock = _smol ? (content) => _smol.parseLockfile(content, "npm", "yarn") : jsParseYarnLock;

//#endregion
exports.addToYarnIndex = addToYarnIndex;
exports.consumeDependenciesMeta = consumeDependenciesMeta;
exports.consumeDependencyList = consumeDependencyList;
exports.consumeEntryProperties = consumeEntryProperties;
exports.jsParseYarnLock = jsParseYarnLock;
exports.newEntry = newEntry;
exports.parseYarnLock = parseYarnLock;
exports.skipIndentedBlock = skipIndentedBlock;
exports.stripQuotes = stripQuotes;
exports.valueAfterKey = valueAfterKey;