"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
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_errors_message = require('../../errors/message.js');
const require_smol_manifest = require('../../smol/manifest.js');
const require_eco_manifest_manifest_error = require('../manifest/manifest-error.js');

//#region src/eco/npm/parse-package-json.ts
/**
* @file `parsePackageJson(content)` — parses a `package.json` string into a
*   `ParsedManifest`. On socket-btm's smol Node binary this routes to
*   `node:smol-manifest`'s native `parseManifest(content, 'npm')`; on stock
*   Node it runs the JS impl below. Both return byte-equivalent frozen shapes.
*   Throws `ManifestError` with code `ERR_INVALID_JSON` on JSON parse failure.
*   Anything else is non-throwing: missing/extra fields, weird types in
*   dep-record values, etc. are accepted — `package.json` in the wild is messy,
*   and downstream consumers can re-validate if they care.
*/
const PROD = "prod";
const DEV = "dev";
const OPTIONAL = "optional";
const PEER = "peer";
function addDeps(dependencies, obj, type) {
	if (!obj || typeof obj !== "object") return;
	const record = obj;
	const keys = require_primordials_object.ObjectKeys(record);
	for (let i = 0, { length } = keys; i < length; i++) {
		const name = keys[i];
		const range = record[name];
		require_primordials_array.ArrayPrototypePush(dependencies, require_primordials_object.ObjectFreeze({
			__proto__: null,
			name,
			versionRange: typeof range === "string" ? range : String(range ?? ""),
			type,
			optional: type === OPTIONAL
		}));
	}
}
function asOptionalString(value) {
	return typeof value === "string" && value.length > 0 ? value : void 0;
}
function jsParsePackageJson(content) {
	let data;
	try {
		data = require_primordials_json.JSONParse(content);
	} catch (e) {
		throw new require_eco_manifest_manifest_error.ManifestError(`Invalid JSON: ${require_errors_message.errorMessage(e)}`, "ERR_INVALID_JSON");
	}
	const dependencies = [];
	addDeps(dependencies, data.dependencies, PROD);
	addDeps(dependencies, data.devDependencies, DEV);
	addDeps(dependencies, data.peerDependencies, PEER);
	addDeps(dependencies, data.optionalDependencies, OPTIONAL);
	return require_primordials_object.ObjectFreeze({
		__proto__: null,
		type: "manifest",
		name: asOptionalString(data.name),
		version: asOptionalString(data.version),
		description: asOptionalString(data.description),
		license: asOptionalString(data.license),
		repository: resolveRepository(data.repository),
		dependencies: require_primordials_object.ObjectFreeze(dependencies),
		ecosystem: "npm"
	});
}
function resolveRepository(value) {
	if (typeof value === "string") return value.length > 0 ? value : void 0;
	if (value && typeof value === "object") {
		const url = value.url;
		if (typeof url === "string" && url.length > 0) return url;
	}
}
const _smol = /* @__PURE__ */ require_smol_manifest.getSmolManifest();
const parsePackageJson = _smol ? (content) => _smol.parseManifest(content, "npm") : jsParsePackageJson;

//#endregion
exports.addDeps = addDeps;
exports.asOptionalString = asOptionalString;
exports.jsParsePackageJson = jsParsePackageJson;
exports.parsePackageJson = parsePackageJson;
exports.resolveRepository = resolveRepository;