import { cfgData, cfgToDOT, cfgToMermaid } from '../features/cfg.js';
import { outputResult } from '../infrastructure/result-formatter.js';
function renderBlockLocation(b) {
    if (!b.startLine)
        return '';
    const endSuffix = b.endLine && b.endLine !== b.startLine ? `-${b.endLine}` : '';
    return ` L${b.startLine}${endSuffix}`;
}
function printCfgBlocks(blocks) {
    if (blocks.length === 0)
        return;
    console.log('\n  Blocks:');
    for (const b of blocks) {
        const label = b.label ? ` (${b.label})` : '';
        console.log(`    [${b.index}] ${b.type}${label}${renderBlockLocation(b)}`);
    }
}
function printCfgEdges(edges) {
    if (edges.length === 0)
        return;
    console.log('\n  Edges:');
    for (const e of edges) {
        console.log(`    B${e.source} → B${e.target}  [${e.kind}]`);
    }
}
function printCfgEntry(r) {
    console.log(`\n${r.kind} ${r.name}  (${r.file}:${r.line})`);
    console.log('─'.repeat(60));
    console.log(`  Blocks: ${r.summary.blockCount}  Edges: ${r.summary.edgeCount}`);
    printCfgBlocks(r.blocks);
    printCfgEdges(r.edges);
}
function tryRenderGraphFormat(format, data) {
    if (format === 'dot') {
        console.log(cfgToDOT(data));
        return true;
    }
    if (format === 'mermaid') {
        console.log(cfgToMermaid(data));
        return true;
    }
    return false;
}
export function cfg(name, customDbPath, opts = {}) {
    const data = cfgData(name, customDbPath, opts);
    if (outputResult(data, 'results', opts))
        return;
    if (data.warning) {
        console.log(`⚠  ${data.warning}`);
        return;
    }
    if (data.results.length === 0) {
        console.log(`No symbols matching "${name}".`);
        return;
    }
    if (tryRenderGraphFormat(opts.format || 'text', data))
        return;
    for (const r of data.results) {
        printCfgEntry(r);
    }
}
//# sourceMappingURL=cfg.js.map