"use strict";
/* Socket Lib - Built with esbuild */
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var normalize_exports = {};
__export(normalize_exports, {
  normalizePackageJson: () => normalizePackageJson,
  resolveEscapedScope: () => resolveEscapedScope,
  resolveOriginalPackageName: () => resolveOriginalPackageName,
  unescapeScope: () => unescapeScope
});
module.exports = __toCommonJS(normalize_exports);
var import_socket = require("../constants/socket");
var import_regexps = require("../regexps");
var import_normalize_package_data = __toESM(require("../external/normalize-package-data"));
var import_objects = require("../objects");
var import_operations = require("./operations");
const ArrayIsArray = Array.isArray;
const ObjectHasOwn = Object.hasOwn;
function getEscapedScopeRegExp() {
  const firstChar = import_socket.REGISTRY_SCOPE_DELIMITER[0];
  return new RegExp(
    `^[^${(0, import_regexps.escapeRegExp)(firstChar)}]+${(0, import_regexps.escapeRegExp)(import_socket.REGISTRY_SCOPE_DELIMITER)}(?!${(0, import_regexps.escapeRegExp)(firstChar)})`
  );
}
// @__NO_SIDE_EFFECTS__
function normalizePackageJson(pkgJson, options) {
  const { preserve } = { __proto__: null, ...options };
  if (!ObjectHasOwn(pkgJson, "version")) {
    pkgJson.version = "0.0.0";
  }
  const preserved = [
    ["_id", void 0],
    ["readme", void 0],
    ...ObjectHasOwn(pkgJson, "bugs") ? [] : [["bugs", void 0]],
    ...ObjectHasOwn(pkgJson, "homepage") ? [] : [["homepage", void 0]],
    ...ObjectHasOwn(pkgJson, "name") ? [] : [["name", void 0]],
    ...ArrayIsArray(preserve) ? preserve.map((k) => [
      k,
      ObjectHasOwn(pkgJson, k) ? pkgJson[k] : void 0
    ]) : []
  ];
  (0, import_normalize_package_data.default)(pkgJson);
  if (pkgJson.name && pkgJson.version) {
    const extensions = (0, import_operations.findPackageExtensions)(pkgJson.name, pkgJson.version);
    if (extensions && typeof extensions === "object") {
      (0, import_objects.merge)(pkgJson, extensions);
    }
  }
  for (const { 0: key, 1: value } of preserved) {
    pkgJson[key] = value;
  }
  return pkgJson;
}
// @__NO_SIDE_EFFECTS__
function resolveEscapedScope(sockRegPkgName) {
  const escapedScopeRegExp = getEscapedScopeRegExp();
  const match = escapedScopeRegExp.exec(sockRegPkgName)?.[0];
  return match || void 0;
}
// @__NO_SIDE_EFFECTS__
function resolveOriginalPackageName(sockRegPkgName) {
  const name = sockRegPkgName.startsWith(`${import_socket.SOCKET_REGISTRY_SCOPE}/`) ? sockRegPkgName.slice(import_socket.SOCKET_REGISTRY_SCOPE.length + 1) : sockRegPkgName;
  const escapedScope = /* @__PURE__ */ resolveEscapedScope(name);
  return escapedScope ? `${/* @__PURE__ */ unescapeScope(escapedScope)}/${name.slice(escapedScope.length)}` : name;
}
// @__NO_SIDE_EFFECTS__
function unescapeScope(escapedScope) {
  return `@${escapedScope.slice(0, -import_socket.REGISTRY_SCOPE_DELIMITER.length)}`;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  normalizePackageJson,
  resolveEscapedScope,
  resolveOriginalPackageName,
  unescapeScope
});
