"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 architecture_exports = {};
__export(architecture_exports, {
  register: () => register
});
module.exports = __toCommonJS(architecture_exports);
var path = __toESM(require("path"));
var import_ui = require("../ui");
function register(program) {
  program.command("architecture [projectPath]").description("Show the detected package graph and architectural layers").option("--format <fmt>", "Output format: markdown, json", "markdown").option("--packages", "Show packages only").option("--layers", "Show layers only").action(async (projectPath, opts) => {
    const KiroGraph = (await Promise.resolve().then(() => require("../../index.js"))).default;
    const target = path.resolve(projectPath ?? process.cwd());
    const cg = await KiroGraph.open(target);
    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();
    if (opts.format === "json") {
      console.log(JSON.stringify(arch, null, 2));
      cg.close();
      return;
    }
    const showPackages = !opts.layers || opts.packages;
    const showLayers = !opts.packages || opts.layers;
    if (showPackages) {
      console.log(`
  ${(0, import_ui.section)("Packages")}  ${import_ui.dim}(${arch.packages.length})${import_ui.reset}
`);
      if (arch.packages.length === 0) {
        console.log(`  ${import_ui.dim}No packages detected.${import_ui.reset}`);
      } else {
        for (const pkg of arch.packages) {
          const src = pkg.source === "manifest" ? `${import_ui.green}manifest${import_ui.reset}` : `${import_ui.dim}directory${import_ui.reset}`;
          const lang = pkg.language ? `  ${import_ui.dim}${pkg.language}${import_ui.reset}` : "";
          const ver = pkg.version ? `  ${import_ui.dim}v${pkg.version}${import_ui.reset}` : "";
          console.log(`  ${import_ui.violet}${import_ui.bold}${pkg.name}${import_ui.reset}${ver}${lang}  ${import_ui.dim}[${src}${import_ui.dim}]${import_ui.reset}`);
          console.log(`  ${import_ui.dim}  path: ${pkg.path}${import_ui.reset}`);
          if (pkg.externalDeps && pkg.externalDeps.length > 0) {
            console.log(`  ${import_ui.dim}  deps: ${pkg.externalDeps.slice(0, 6).join(", ")}${pkg.externalDeps.length > 6 ? ` +${pkg.externalDeps.length - 6} more` : ""}${import_ui.reset}`);
          }
        }
      }
      if (arch.packageDeps.length > 0) {
        console.log(`
  ${(0, import_ui.section)("Package Dependencies")}  ${import_ui.dim}(${arch.packageDeps.length} edges)${import_ui.reset}
`);
        for (const dep of arch.packageDeps) {
          const from = dep.sourcePkg.replace(/^pkg:[^:]+:/, "");
          const to = dep.targetPkg.replace(/^pkg:[^:]+:/, "");
          console.log(`  ${import_ui.violet}${from}${import_ui.reset}  ${import_ui.dim}\u2192${import_ui.reset}  ${import_ui.violet}${to}${import_ui.reset}  ${import_ui.dim}(${dep.depCount} import${dep.depCount !== 1 ? "s" : ""})${import_ui.reset}`);
        }
      }
    }
    if (showLayers) {
      console.log(`
  ${(0, import_ui.section)("Layers")}  ${import_ui.dim}(${arch.layers.length})${import_ui.reset}
`);
      if (arch.layers.length === 0) {
        console.log(`  ${import_ui.dim}No layers detected.${import_ui.reset}`);
      } else {
        const layerFileCounts = {};
        for (const assignments of Object.values(arch.fileLayers)) {
          for (const a of assignments) {
            layerFileCounts[a.layerId] = (layerFileCounts[a.layerId] ?? 0) + 1;
          }
        }
        const rows = arch.layers.map((l) => [
          l.name,
          `${layerFileCounts[l.id] ?? 0} files`
        ]);
        console.log((0, import_ui.renderTable)(rows));
      }
      if (arch.layerDeps.length > 0) {
        console.log(`
  ${(0, import_ui.section)("Layer Dependencies")}  ${import_ui.dim}(${arch.layerDeps.length} edges)${import_ui.reset}
`);
        for (const dep of arch.layerDeps) {
          const from = dep.sourceLayer.replace(/^layer:/, "");
          const to = dep.targetLayer.replace(/^layer:/, "");
          console.log(`  ${import_ui.violet}${from}${import_ui.reset}  ${import_ui.dim}\u2192${import_ui.reset}  ${import_ui.violet}${to}${import_ui.reset}  ${import_ui.dim}(${dep.depCount})${import_ui.reset}`);
        }
      }
    }
    console.log();
    cg.close();
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  register
});
//# sourceMappingURL=architecture.js.map
