"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
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 arborist_exports = {};
__export(arborist_exports, {
  getBaseArboristOptions: () => getBaseArboristOptions,
  readSingleDependency: () => readSingleDependency,
  readTopLevelFromIdealTree: () => readTopLevelFromIdealTree,
  safeIdealTree: () => safeIdealTree,
  safeReify: () => safeReify,
  writeSafeNpmrc: () => writeSafeNpmrc
});
module.exports = __toCommonJS(arborist_exports);
var import_arborist = __toESM(require("../external/@npmcli/arborist"));
var import_socket = require("../paths/socket");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_json = require("../primordials/json");
var import_object = require("../primordials/object");
var import_fs = require("../node/fs");
var import_path = require("../node/path");
function getBaseArboristOptions(installPath, options) {
  return {
    __proto__: null,
    path: installPath,
    cache: (0, import_socket.getSocketCacacheDir)(),
    audit: false,
    fund: false,
    ignoreScripts: true,
    progress: false,
    save: false,
    saveBundle: false,
    silent: options.quiet
  };
}
function readSingleDependency(packageJsonPath) {
  const fs = (0, import_fs.getNodeFs)();
  const raw = fs.readFileSync(packageJsonPath, "utf8");
  const pkg = (0, import_json.JSONParse)(raw);
  const deps = pkg.dependencies ?? {};
  const names = (0, import_object.ObjectKeys)(deps);
  if (names.length !== 1) {
    throw new import_error.ErrorCtor(
      `safeIdealTree expects exactly one top-level dependency in ${packageJsonPath}, found ${names.length}`
    );
  }
  return names[0];
}
function readTopLevelFromIdealTree(tree, targetName) {
  const root = tree;
  const inventory = root?.inventory;
  if (!inventory || typeof inventory.values !== "function") {
    throw new import_error.ErrorCtor("Arborist idealTree missing inventory");
  }
  for (const node of inventory.values()) {
    if (node.isProjectRoot) {
      continue;
    }
    if (node.name === targetName && node.depth === 1) {
      if (!node.version || !node.integrity) {
        throw new import_error.ErrorCtor(
          `Arborist idealTree node for ${targetName} missing version/integrity`
        );
      }
      return {
        name: node.name,
        version: node.version,
        integrity: node.integrity
      };
    }
  }
  throw new import_error.ErrorCtor(
    `Arborist idealTree inventory has no top-level node for ${targetName}`
  );
}
async function safeIdealTree(options) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const { before, path: installPath, quiet = true } = options;
  const targetName = readSingleDependency(
    path.join(installPath, "package.json")
  );
  const arb = new import_arborist.default({
    ...getBaseArboristOptions(installPath, { quiet }),
    ...before !== void 0 ? { before } : {},
    packageLockOnly: true,
    save: true
  });
  const tree = await arb.buildIdealTree();
  await arb.reify();
  const top = readTopLevelFromIdealTree(tree, targetName);
  const lockfile = await fs.promises.readFile(
    path.join(installPath, "package-lock.json"),
    "utf8"
  );
  return { ...top, lockfile };
}
async function safeReify(options) {
  const { packageLock = true, path: installPath, quiet = true } = options;
  const arb = new import_arborist.default({
    ...getBaseArboristOptions(installPath, { quiet }),
    packageLock
  });
  await arb.reify();
}
async function writeSafeNpmrc(installPath, options) {
  const fs = (0, import_fs.getNodeFs)();
  const path = (0, import_path.getNodePath)();
  const { minReleaseDays, minReleaseMins } = {
    __proto__: null,
    ...options
  };
  if (minReleaseDays !== void 0 && minReleaseMins !== void 0) {
    throw new import_error.ErrorCtor(
      "writeSafeNpmrc: minReleaseDays and minReleaseMins are mutually exclusive"
    );
  }
  const lines = [
    "ignore-scripts=true",
    "audit=false",
    "fund=false",
    "save=false",
    "save-bundle=false",
    "progress=false"
  ];
  if (minReleaseDays !== void 0) {
    (0, import_array.ArrayPrototypePush)(lines, `min-release-age=${minReleaseDays}`);
  }
  if (minReleaseMins !== void 0) {
    (0, import_array.ArrayPrototypePush)(lines, `minimum-release-age=${minReleaseMins}`);
  }
  await fs.promises.writeFile(
    path.join(installPath, ".npmrc"),
    lines.join("\n") + "\n",
    "utf8"
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getBaseArboristOptions,
  readSingleDependency,
  readTopLevelFromIdealTree,
  safeIdealTree,
  safeReify,
  writeSafeNpmrc
});
