// engine/mermaid/core.ts — Main orchestrator for Mermaid diagram generation.
// Extracted from mermaid-generator.ts to keep file sizes within limits.
import { isScopeAtLeast, buildLayers } from './helpers.js';
import { generateErDiagram, generateSequenceDiagram, generateComponentDiagram, generateStateDiagram, generateArchitectureDiagram, } from './diagram-generators.js';
import { generateClassDiagram, generateGanttDiagram, generatePieDiagram, } from '../mermaid-extended.js';
/**
 * Generate all applicable diagrams for a spec based on its scope and context.
 */
export function generateDiagrams(spec, knowledge, options) {
    const diagrams = [];
    // Data flow diagram for ALL specs (except trivial)
    if (spec.scope !== 'trivial') {
        diagrams.push(generateDataFlowDiagram(spec, knowledge));
    }
    // ER diagram if database changes
    if (options.hasDatabase) {
        diagrams.push(generateErDiagram(spec, knowledge));
    }
    // Sequence diagram if scope >= cross-module
    if (isScopeAtLeast(spec.scope, 'cross-module')) {
        diagrams.push(generateSequenceDiagram(spec, knowledge));
    }
    // Component diagram if UI
    if (options.hasUi) {
        diagrams.push(generateComponentDiagram(spec, knowledge));
    }
    // State machine diagram if transitions
    if (options.hasTransitions) {
        diagrams.push(generateStateDiagram(spec));
    }
    // Architecture diagram if scope = architectural
    if (spec.scope === 'architectural') {
        diagrams.push(generateArchitectureDiagram(spec, knowledge));
    }
    // Class diagram for cross-module+ backend/shared specs
    if (isScopeAtLeast(spec.scope, 'cross-module') &&
        (spec.target === 'backend' || spec.target === 'shared' || spec.target === 'fullstack')) {
        diagrams.push(generateClassDiagram(spec));
    }
    // Gantt diagram for larger specs (> 8 dev hours)
    if (spec.estimation.devHours > 8) {
        diagrams.push(generateGanttDiagram(spec));
    }
    // Pie chart for effort distribution (always for per-spec reports)
    diagrams.push(generatePieDiagram(spec));
    return diagrams;
}
/**
 * Generate a data flow diagram: Client -> Action -> Service -> Repo -> DB
 */
export function generateDataFlowDiagram(spec, knowledge) {
    const layers = buildLayers(spec.target, knowledge);
    const lines = ['graph LR'];
    layers.forEach((from, i) => {
        const to = layers[i + 1];
        if (to) {
            lines.push(`  ${from.id}[${from.label}] --> ${to.id}[${to.label}]`);
        }
    });
    return {
        type: 'data-flow',
        title: `Data Flow — ${spec.title}`,
        format: 'mermaid',
        content: lines.join('\n'),
        description: 'Data flow diagram showing how data moves through the system layers.',
    };
}
/**
 * Format diagrams as Mermaid code blocks for inclusion in markdown.
 */
export function formatDiagramsAsMarkdown(diagrams) {
    if (diagrams.length === 0) {
        return '';
    }
    const sections = [];
    for (const diagram of diagrams) {
        sections.push(`### ${diagram.title}`);
        sections.push('');
        sections.push(`> ${diagram.description}`);
        sections.push('');
        sections.push('```mermaid');
        sections.push(diagram.content);
        sections.push('```');
        sections.push('');
    }
    return sections.join('\n');
}
//# sourceMappingURL=core.js.map