"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var parse_package_json_exports = {};
__export(parse_package_json_exports, {
  addDeps: () => addDeps,
  asOptionalString: () => asOptionalString,
  jsParsePackageJson: () => jsParsePackageJson,
  parsePackageJson: () => parsePackageJson,
  resolveRepository: () => resolveRepository
});
module.exports = __toCommonJS(parse_package_json_exports);
var import_manifest_error = require("../manifest/manifest-error");
var import_message = require("../../errors/message");
var import_array = require("../../primordials/array");
var import_json = require("../../primordials/json");
var import_object = require("../../primordials/object");
var import_manifest = require("../../smol/manifest");
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 = (0, import_object.ObjectKeys)(record);
  for (let i = 0, { length } = keys; i < length; i++) {
    const name = keys[i];
    const range = record[name];
    (0, import_array.ArrayPrototypePush)(
      dependencies,
      (0, import_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 = (0, import_json.JSONParse)(content);
  } catch (e) {
    throw new import_manifest_error.ManifestError(
      `Invalid JSON: ${(0, import_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 (0, import_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: (0, import_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;
    }
  }
  return void 0;
}
const _smol = (0, import_manifest.getSmolManifest)();
const parsePackageJson = _smol ? (
  /* c8 ignore next 1 - smol Node binary only. */
  (content) => _smol.parseManifest(content, "npm")
) : jsParsePackageJson;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  addDeps,
  asOptionalString,
  jsParsePackageJson,
  parsePackageJson,
  resolveRepository
});
