"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_string = require('../primordials/string.js');
let src_external_npm_package_arg = require("../external/npm-package-arg");
src_external_npm_package_arg = require_runtime.__toESM(src_external_npm_package_arg);

//#region src/dlx/spec.ts
/**
* @file Parse `package@version` specs into `{name, version}`. Split out of
*   `dlx/package.ts` for size hygiene. Uses npm-package-arg for the full
*   grammar; falls back to a hand-rolled last-`@` parser when npm-package-arg
*   can't handle the input.
*/
/**
* Parse package spec into name and version using npm-package-arg. Examples: -
* 'lodash@4.17.21' → { name: 'lodash', version: '4.17.21' } -
* '@scope/pkg@1.0.0' → { name: '@scope/pkg', version: '1.0.0' } - 'lodash' → {
* name: 'lodash', version: undefined }
*
* @example
*   ;```typescript
*   parsePackageSpec('lodash@4.17.21')
*   // { name: 'lodash', version: '4.17.21' }
*
*   parsePackageSpec('@scope/pkg')
*   // { name: '@scope/pkg', version: undefined }
*   ```
*/
function parsePackageSpec(spec) {
	try {
		/* c8 ignore next - External npm-package-arg call */
		const parsed = (0, src_external_npm_package_arg.default)(spec);
		const version = parsed.type === "tag" ? parsed.fetchSpec : parsed.type === "version" || parsed.type === "range" ? parsed.fetchSpec : void 0;
		return {
			name: parsed.name || spec,
			version
		};
	} catch {
		const atIndex = require_primordials_string.StringPrototypeLastIndexOf(spec, "@");
		if (atIndex === -1 || atIndex === 0) return {
			name: spec,
			version: void 0
		};
		const sliced = require_primordials_string.StringPrototypeSlice(spec, atIndex + 1);
		return {
			name: require_primordials_string.StringPrototypeSlice(spec, 0, atIndex),
			version: sliced || void 0
		};
	}
}

//#endregion
exports.parsePackageSpec = parsePackageSpec;