import { defineCommand } from '@kb-labs/sdk';
import { buildDependencyGraph, findCircularDependencies } from '@kb-labs/quality-core/graph';

// src/cli/commands/cycles.ts
var cycles_default = defineCommand({
  id: "quality:cycles",
  description: "Detect circular dependencies in monorepo",
  handler: {
    async execute(ctx, input) {
      const { ui } = ctx;
      const flags = input.flags ?? input;
      const graph = buildDependencyGraph(ctx.cwd);
      const cycles = findCircularDependencies(graph);
      outputCycles(cycles, flags, ui);
      return {
        exitCode: cycles.length > 0 ? 1 : 0,
        cycles
      };
    }
  }
});
function outputCycles(cycles, flags, ui) {
  if (flags.json) {
    ui?.json?.({ cycles, count: cycles.length });
    return;
  }
  if (cycles.length === 0) {
    ui?.success?.("No circular dependencies found", {
      title: "\u2705 Circular Dependencies Check",
      sections: [
        {
          header: "Result",
          items: ["\u2705 No circular dependencies detected"]
        }
      ]
    });
    return;
  }
  const sections = [];
  sections.push({
    header: "Summary",
    items: [
      `\u26A0\uFE0F Found ${cycles.length} circular dependency chain(s)`,
      "These must be broken to enable clean builds"
    ]
  });
  for (let i = 0; i < cycles.length; i++) {
    const cycle = cycles[i];
    if (!cycle) {
      continue;
    }
    const cycleItems = [];
    for (let j = 0; j < cycle.length; j++) {
      const pkg = cycle[j];
      if (j === cycle.length - 1) {
        cycleItems.push(`  ${j + 1}. ${pkg}`);
        const firstPkg = cycle[0];
        cycleItems.push(`  ${j + 2}. ${firstPkg ?? "?"} \u2B05 circular!`);
      } else {
        cycleItems.push(`  ${j + 1}. ${pkg}`);
      }
    }
    sections.push({
      header: `Cycle ${i + 1} (${cycle.length} packages)`,
      items: cycleItems
    });
  }
  sections.push({
    header: "Recommendations",
    items: [
      "1. Extract shared code into a new package",
      "2. Move one dependency to devDependencies",
      "3. Refactor to remove bidirectional dependencies",
      "4. Use dependency injection patterns"
    ]
  });
  ui?.error?.("Circular dependencies detected", {
    title: `\u26A0\uFE0F Circular Dependencies Detected (${cycles.length})`,
    sections
  });
}

export { cycles_default as default };
//# sourceMappingURL=cycles.js.map
//# sourceMappingURL=cycles.js.map