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

//#region src/eco/npm/yarnpkg/yarn/parse-yarn-descriptor.ts
/**
* @file `parseYarnDescriptor(descriptor)` — extracts the package name from a
*   yarn lockfile spec key. Spec formats handled:
*
*   - Yarn Classic: `name@^1.0.0` → `name`
*   - Berry npm: `name@npm:^1.0.0` → `name`
*   - Berry workspace: `name@workspace:^1.0.0` → `name`
*   - Berry patch: `patch:name@npm:1.0.0#…` → `name`
*   - Berry patch (encoded): `patch:name@npm%3A1.0.0#…` → `name`
*   - Scoped: `@scope/name@^1.0.0` → `@scope/name` Matches socket-btm's
*     smol-manifest internal `parseYarnDescriptor`.
*/
const PATCH_PREFIX = "patch:";
const PATCH_PREFIX_LEN = 6;
const NPM_PROTO = "@npm:";
const NPM_PROTO_ENCODED = "@npm%3A";
const WORKSPACE_PROTO = "@workspace:";
function parseYarnDescriptor(descriptor) {
	if (require_primordials_string.StringPrototypeIndexOf(descriptor, PATCH_PREFIX) === 0) {
		const afterPatch = require_primordials_string.StringPrototypeSlice(descriptor, PATCH_PREFIX_LEN);
		let npmIndex = require_primordials_string.StringPrototypeIndexOf(afterPatch, NPM_PROTO);
		const npmEncodedIndex = require_primordials_string.StringPrototypeIndexOf(afterPatch, NPM_PROTO_ENCODED);
		const workspaceIndex = require_primordials_string.StringPrototypeIndexOf(afterPatch, WORKSPACE_PROTO);
		if (npmEncodedIndex > 0 && (npmIndex === -1 || npmEncodedIndex < npmIndex)) npmIndex = npmEncodedIndex;
		if (npmIndex > 0) return { name: require_primordials_string.StringPrototypeSlice(afterPatch, 0, npmIndex) };
		if (workspaceIndex > 0) return { name: require_primordials_string.StringPrototypeSlice(afterPatch, 0, workspaceIndex) };
	}
	let protocolIndex = require_primordials_string.StringPrototypeIndexOf(descriptor, NPM_PROTO);
	if (protocolIndex > 0) return { name: require_primordials_string.StringPrototypeSlice(descriptor, 0, protocolIndex) };
	protocolIndex = require_primordials_string.StringPrototypeIndexOf(descriptor, WORKSPACE_PROTO);
	if (protocolIndex > 0) return { name: require_primordials_string.StringPrototypeSlice(descriptor, 0, protocolIndex) };
	const atIdx = require_primordials_string.StringPrototypeLastIndexOf(descriptor, "@");
	if (atIdx > 0) return { name: require_primordials_string.StringPrototypeSlice(descriptor, 0, atIdx) };
	return { name: descriptor };
}

//#endregion
exports.parseYarnDescriptor = parseYarnDescriptor;