"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');
const require_primordials_regexp = require('../primordials/regexp.js');
const require_primordials_array = require('../primordials/array.js');
const require_primordials_object = require('../primordials/object.js');
const require_objects_mutate = require('../objects/mutate.js');
const require_constants_socket = require('../constants/socket.js');
const require_regexps_escape = require('../regexps/escape.js');
const require_packages_operations = require('./operations.js');
let src_external_normalize_package_data = require("../external/normalize-package-data");
src_external_normalize_package_data = require_runtime.__toESM(src_external_normalize_package_data);

//#region src/packages/normalize.ts
/**
* @file Package.json normalization utilities.
*/
function getEscapedScopeRegExp() {
	const firstChar = "__"[0];
	return new require_primordials_regexp.RegExpCtor(`^[^${require_regexps_escape.escapeRegExp(firstChar)}]+${require_regexps_escape.escapeRegExp("__")}(?!${require_regexps_escape.escapeRegExp(firstChar)})`);
}
/**
* Normalize a package.json object with standard npm package normalization.
*
* @example
*   ;```typescript
*   const pkgJson = { name: 'my-pkg', version: '1.0.0' }
*   const normalized = normalizePackageJson(pkgJson)
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function normalizePackageJson(pkgJson, options) {
	const { preserve } = {
		__proto__: null,
		...options
	};
	if (!require_primordials_object.ObjectHasOwn(pkgJson, "version")) pkgJson.version = "0.0.0";
	const preserved = [
		["_id", void 0],
		["readme", void 0],
		...require_primordials_object.ObjectHasOwn(pkgJson, "bugs") ? [] : [["bugs", void 0]],
		...require_primordials_object.ObjectHasOwn(pkgJson, "homepage") ? [] : [["homepage", void 0]],
		...require_primordials_object.ObjectHasOwn(pkgJson, "name") ? [] : [["name", void 0]],
		...require_primordials_array.ArrayIsArray(preserve) ? preserve.map((k) => [k, require_primordials_object.ObjectHasOwn(pkgJson, k) ? pkgJson[k] : void 0]) : []
	];
	(0, src_external_normalize_package_data.default)(pkgJson);
	if (pkgJson.name && pkgJson.version) {
		const extensions = /* @__PURE__ */ require_packages_operations.findPackageExtensions(pkgJson.name, pkgJson.version);
		if (extensions && typeof extensions === "object") /* @__PURE__ */ require_objects_mutate.merge(pkgJson, extensions);
	}
	for (const { 0: key, 1: value } of preserved) pkgJson[key] = value;
	return pkgJson;
}
/**
* Extract escaped scope from a Socket registry package name.
*
* @example
*   ;```typescript
*   resolveEscapedScope('babel__core') // 'babel__'
*   resolveEscapedScope('lodash') // undefined
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function resolveEscapedScope(sockRegPkgName) {
	return require_primordials_regexp.RegExpPrototypeExec(getEscapedScopeRegExp(), sockRegPkgName)?.[0] || void 0;
}
/**
* Resolve original package name from Socket registry package name.
*
* @example
*   ;```typescript
*   resolveOriginalPackageName('@socketregistry/is-number') // 'is-number'
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function resolveOriginalPackageName(sockRegPkgName) {
	const name = require_primordials_string.StringPrototypeStartsWith(sockRegPkgName, `${"@socketregistry"}/`) ? sockRegPkgName.slice(require_constants_socket.SOCKET_REGISTRY_SCOPE.length + 1) : sockRegPkgName;
	const escapedScope = /* @__PURE__ */ resolveEscapedScope(name);
	return escapedScope ? `${/* @__PURE__ */ unescapeScope(escapedScope)}/${require_primordials_string.StringPrototypeSlice(name, escapedScope.length)}` : name;
}
/**
* Convert escaped scope to standard npm scope format.
*
* @example
*   ;```typescript
*   unescapeScope('babel__') // '@babel'
*   ```
*/
/*@__NO_SIDE_EFFECTS__*/
function unescapeScope(escapedScope) {
	if (escapedScope.length < "__".length) return `@${escapedScope}`;
	return `@${escapedScope.slice(0, -"__".length)}`;
}

//#endregion
exports.getEscapedScopeRegExp = getEscapedScopeRegExp;
exports.normalizePackageJson = normalizePackageJson;
exports.resolveEscapedScope = resolveEscapedScope;
exports.resolveOriginalPackageName = resolveOriginalPackageName;
exports.unescapeScope = unescapeScope;