// tools/analyze-spec-dependencies.ts — Handler for analyze_spec_dependencies tool.
// Builds a dependency graph from project specs and returns analysis with Mermaid diagram.
import { listSpecs } from '../storage/spec-store.js';
import { analyzeSpecDependencies } from '../engine/spec-dependency-graph.js';
/**
 * Analyze spec dependencies: critical path, cycles, execution waves, Mermaid diagram.
 */
export async function handleAnalyzeSpecDependencies(args) {
    const allSpecs = await listSpecs(args.projectId);
    if (allSpecs.length === 0) {
        return {
            content: [
                {
                    type: 'text',
                    text: `No specs found for project "${args.projectId}". Create specs first with create_spec.`,
                },
            ],
            isError: true,
        };
    }
    // Filter to specific specs if requested
    const specIds = args.specIds;
    const specs = specIds ? allSpecs.filter((s) => specIds.includes(s.id)) : allSpecs;
    if (specs.length === 0) {
        return {
            content: [
                {
                    type: 'text',
                    text: `None of the specified spec IDs were found in project "${args.projectId}".`,
                },
            ],
            isError: true,
        };
    }
    const analysis = analyzeSpecDependencies(specs);
    const sections = [];
    // Summary
    sections.push(`## Spec Dependency Analysis\n`);
    sections.push(`- **Total specs**: ${analysis.totalSpecs}`);
    sections.push(`- **Total dependencies**: ${analysis.totalEdges}`);
    // Critical path
    if (analysis.criticalPath.length > 0) {
        sections.push(`\n### Critical Path (${analysis.criticalPathHours}h)`);
        sections.push(analysis.criticalPath.map((id) => `  ${id}`).join(' -> '));
    }
    else {
        sections.push(`\n### Critical Path\nNo dependency chains found.`);
    }
    // Cycles
    if (analysis.cycles.length > 0) {
        sections.push(`\n### Circular Dependencies (${analysis.cycles.length})`);
        for (const cycle of analysis.cycles) {
            sections.push(`- ${cycle.join(' -> ')}`);
        }
    }
    // Execution waves
    if (analysis.executionWaves.length > 0) {
        sections.push(`\n### Execution Waves`);
        for (let i = 0; i < analysis.executionWaves.length; i++) {
            const wave = analysis.executionWaves[i];
            if (wave) {
                sections.push(`**Wave ${i + 1}**: ${wave.join(', ')}`);
            }
        }
    }
    // Mermaid diagram
    sections.push(`\n### Dependency Graph\n\`\`\`mermaid\n${analysis.mermaidDiagram}\n\`\`\``);
    return {
        content: [{ type: 'text', text: sections.join('\n') }],
    };
}
//# sourceMappingURL=analyze-spec-dependencies.js.map