"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 package_exports = {};
__export(package_exports, {
  register: () => register
});
module.exports = __toCommonJS(package_exports);
var import_ui = require("../ui");
function register(program) {
  program.command("package <name>").description("Inspect a package: files, coupling, and dependencies").option("--no-files", "Omit file list").option("--format <fmt>", "Output format: markdown, json", "markdown").action(async (name, opts, cmd) => {
    const KiroGraph = (await Promise.resolve().then(() => require("../../index.js"))).default;
    const cg = await KiroGraph.open(process.cwd());
    if (!cg.isArchitectureEnabled()) {
      console.error(`
  ${"\x1B[33m"}\u26A0 Architecture analysis is disabled.${import_ui.reset}`);
      console.error(`  ${import_ui.dim}Enable it in .kirograph/config.json:${import_ui.reset} ${import_ui.violet}${import_ui.bold}"enableArchitecture": true${import_ui.reset}`);
      console.error(`  ${import_ui.dim}Then re-run:${import_ui.reset} ${import_ui.violet}${import_ui.bold}kirograph index${import_ui.reset}
`);
      cg.close();
      process.exit(1);
    }
    const arch = cg.getArchitecture();
    const query = name.toLowerCase();
    const pkg = arch.packages.find(
      (p) => p.name.toLowerCase().includes(query) || p.path.toLowerCase().includes(query) || p.id.toLowerCase().includes(query)
    );
    if (!pkg) {
      console.error(`
  ${"\x1B[38;5;203m"}\u2717 Package "${name}" not found.${import_ui.reset}`);
      console.error(`  ${import_ui.dim}Run ${import_ui.reset}${import_ui.violet}${import_ui.bold}kirograph architecture --packages${import_ui.reset}${import_ui.dim} to list all packages.${import_ui.reset}
`);
      cg.close();
      process.exit(1);
    }
    if (opts.format === "json") {
      const deps2 = arch.packageDeps.filter((d) => d.sourcePkg === pkg.id);
      const dependents2 = arch.packageDeps.filter((d) => d.targetPkg === pkg.id);
      const coupling2 = arch.coupling.find((c) => c.packageId === pkg.id);
      const files = Object.entries(arch.filePackages).filter(([, ids]) => ids.includes(pkg.id)).map(([f]) => f).sort();
      console.log(JSON.stringify({ package: pkg, coupling: coupling2, deps: deps2, dependents: dependents2, files }, null, 2));
      cg.close();
      return;
    }
    const srcLabel = pkg.source === "manifest" ? `${import_ui.green}manifest${import_ui.reset}` : `${import_ui.dim}directory${import_ui.reset}`;
    console.log(`
  ${(0, import_ui.section)(pkg.name)}  ${import_ui.dim}${pkg.path}${import_ui.reset}`);
    console.log(`  ${import_ui.dim}id: ${pkg.id}${import_ui.reset}
`);
    const meta = [
      ["Source", pkg.source === "manifest" ? "manifest" : "directory"]
    ];
    if (pkg.language) meta.push(["Language", pkg.language]);
    if (pkg.version) meta.push(["Version", pkg.version]);
    if (pkg.manifestPath) meta.push(["Manifest", pkg.manifestPath]);
    console.log((0, import_ui.renderTable)(meta));
    const coupling = arch.coupling.find((c) => c.packageId === pkg.id);
    if (coupling) {
      console.log(`
  ${(0, import_ui.section)("Coupling")}
`);
      console.log((0, import_ui.renderTable)([
        ["Afferent Ca", String(coupling.afferent)],
        ["Efferent Ce", String(coupling.efferent)],
        ["Instability", `${(coupling.instability * 100).toFixed(0)}%`]
      ]));
    }
    const deps = arch.packageDeps.filter((d) => d.sourcePkg === pkg.id);
    if (deps.length > 0) {
      console.log(`
  ${(0, import_ui.label)("Depends on")}  ${import_ui.dim}(${deps.length})${import_ui.reset}
`);
      for (const dep of deps) {
        const depName = arch.packages.find((p) => p.id === dep.targetPkg)?.name ?? dep.targetPkg;
        console.log(`  ${import_ui.dim}\u2192${import_ui.reset}  ${import_ui.violet}${depName}${import_ui.reset}  ${import_ui.dim}(${dep.depCount} import${dep.depCount !== 1 ? "s" : ""})${import_ui.reset}`);
      }
    }
    const dependents = arch.packageDeps.filter((d) => d.targetPkg === pkg.id);
    if (dependents.length > 0) {
      console.log(`
  ${(0, import_ui.label)("Depended on by")}  ${import_ui.dim}(${dependents.length})${import_ui.reset}
`);
      for (const dep of dependents) {
        const depName = arch.packages.find((p) => p.id === dep.sourcePkg)?.name ?? dep.sourcePkg;
        console.log(`  ${import_ui.dim}\u2190${import_ui.reset}  ${import_ui.violet}${depName}${import_ui.reset}  ${import_ui.dim}(${dep.depCount} import${dep.depCount !== 1 ? "s" : ""})${import_ui.reset}`);
      }
    }
    if (pkg.externalDeps && pkg.externalDeps.length > 0) {
      console.log(`
  ${(0, import_ui.label)("External deps")}  ${import_ui.dim}(${pkg.externalDeps.length})${import_ui.reset}
`);
      const chunks = [];
      for (let i = 0; i < Math.min(pkg.externalDeps.length, 30); i += 4) {
        chunks.push("  " + pkg.externalDeps.slice(i, i + 4).map((d) => `${import_ui.violet}${d}${import_ui.reset}`).join(`  ${import_ui.dim}\xB7${import_ui.reset}  `));
      }
      console.log(chunks.join("\n"));
      if (pkg.externalDeps.length > 30) {
        console.log(`  ${import_ui.dim}\u2026and ${pkg.externalDeps.length - 30} more${import_ui.reset}`);
      }
    }
    if (opts.files) {
      const files = Object.entries(arch.filePackages).filter(([, ids]) => ids.includes(pkg.id)).map(([f]) => f).sort();
      if (files.length > 0) {
        console.log(`
  ${(0, import_ui.label)("Files")}  ${import_ui.dim}(${files.length})${import_ui.reset}
`);
        const shown = files.slice(0, 40);
        for (const f of shown) {
          console.log(`  ${import_ui.dim}${f}${import_ui.reset}`);
        }
        if (files.length > 40) {
          console.log(`  ${import_ui.dim}\u2026and ${files.length - 40} more${import_ui.reset}`);
        }
      }
    }
    console.log();
    cg.close();
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  register
});
//# sourceMappingURL=package.js.map
