"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 npm_exports = {};
__export(npm_exports, {
  npmParser: () => npmParser
});
module.exports = __toCommonJS(npm_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
const npmParser = {
  name: "npm",
  manifestFiles: ["package.json"],
  language: "typescript",
  canParse(manifestPath) {
    return path.basename(manifestPath) === "package.json";
  },
  async parse(manifestPath, projectRoot) {
    let raw;
    try {
      raw = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
    } catch {
      return [];
    }
    if (typeof raw !== "object" || raw === null) return [];
    const pkg = raw;
    const name = typeof pkg.name === "string" ? pkg.name : path.basename(path.dirname(manifestPath));
    const version = typeof pkg.version === "string" ? pkg.version : void 0;
    const relDir = path.relative(projectRoot, path.dirname(manifestPath)).replace(/\\/g, "/") || ".";
    const relManifest = path.relative(projectRoot, manifestPath).replace(/\\/g, "/");
    const externalDeps = _collectDeps(pkg);
    const packages = [{
      id: `pkg:npm:${relDir === "." ? name : relDir}`,
      name,
      path: relDir,
      source: "manifest",
      language: "typescript",
      manifestPath: relManifest,
      version,
      externalDeps,
      updatedAt: Date.now()
    }];
    const workspaces = _getWorkspaces(pkg);
    for (const ws of workspaces) {
      const wsDir = path.join(path.dirname(manifestPath), ws);
      const wsPkg = path.join(wsDir, "package.json");
      if (fs.existsSync(wsPkg)) {
        const sub = await this.parse(wsPkg, projectRoot);
        packages.push(...sub);
      }
    }
    return packages;
  }
};
function _collectDeps(pkg) {
  const deps = /* @__PURE__ */ new Set();
  for (const field of ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]) {
    const d = pkg[field];
    if (typeof d === "object" && d !== null) {
      for (const k of Object.keys(d)) deps.add(k);
    }
  }
  return [...deps];
}
function _getWorkspaces(pkg) {
  const ws = pkg.workspaces;
  if (Array.isArray(ws)) return ws.filter((w) => typeof w === "string");
  if (typeof ws === "object" && ws !== null) {
    const packages = ws.packages;
    if (Array.isArray(packages)) return packages.filter((w) => typeof w === "string");
  }
  return [];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  npmParser
});
//# sourceMappingURL=npm.js.map
