import path from 'node:path';
import { parsePositiveInt } from '../cli-validation.js';
import { writeStream } from '../utils.js';
import { addEntityBoundsCliOptions, entityBoundsFromCli, } from './entity-bounds-command.js';
import { exportEntityGraph } from './entity-graph.js';
function optionalPositive(value, label) {
    return value === undefined ? undefined : parsePositiveInt(value, 1, label);
}
/** Register the immutable whole-repository entity graph export. */
export function registerEntityGraphCommand(code) {
    const command = code
        .command('graph-export')
        .description('Stream the pinned entity co-change, incidence, and seam graph as NDJSON')
        .requiredOption('--path <repo-root>', 'repository root')
        .requiredOption('--ref <ref>', 'pinned commit to observe')
        .option('--no-formats', 'exclude JSON/YAML/TOML/CSV/Markdown entities')
        .option('--max-nodes <n>', 'maximum observed lineage nodes retained')
        .option('--max-edges <n>', 'maximum unified entity-pair edges retained')
        .option('--max-seams <n>', 'maximum pinned import/call module seams retained')
        .option('--max-entities-per-transition <n>', 'skip all-to-all co-change for broader transitions')
        .option('--max-structure-files <n>', 'maximum pinned Git tree files structurally inspected')
        .option('--require-complete', 'exit nonzero when the footer reports incomplete coverage');
    addEntityBoundsCliOptions(command);
    command.action(async (options) => {
        try {
            const maxNodes = optionalPositive(options.maxNodes, '--max-nodes');
            const maxEdges = optionalPositive(options.maxEdges, '--max-edges');
            const maxSeams = optionalPositive(options.maxSeams, '--max-seams');
            const maxEntitiesPerTransition = optionalPositive(options.maxEntitiesPerTransition, '--max-entities-per-transition');
            const maxStructureFiles = optionalPositive(options.maxStructureFiles, '--max-structure-files');
            const request = {
                repoPath: path.resolve(options.path),
                ref: options.ref,
                formats: options.formats,
                bounds: entityBoundsFromCli(options),
                ...(maxNodes !== undefined ? { maxNodes } : {}),
                ...(maxEdges !== undefined ? { maxEdges } : {}),
                ...(maxSeams !== undefined ? { maxSeams } : {}),
                ...(maxEntitiesPerTransition !== undefined ? { maxEntitiesPerTransition } : {}),
                ...(maxStructureFiles !== undefined ? { maxStructureFiles } : {}),
            };
            let complete = false;
            for await (const event of exportEntityGraph(request)) {
                await writeStream(process.stdout, `${JSON.stringify(event)}\n`);
                if (event.type === 'footer')
                    complete = event.complete;
            }
            if (options.requireComplete && !complete)
                process.exitCode = 1;
        }
        catch (error) {
            process.exitCode = 1;
            await writeStream(process.stderr, `Error: ${error instanceof Error ? error.message : String(error)}\n`);
        }
    });
}
//# sourceMappingURL=entity-graph-command.js.map