/**
 * `olam flywheel k10-measure` — K10 token-budget measurement CLI.
 *
 * Reads `--upstream <path>` + optional `--overlay <path>`; counts tokens via
 * the len/CHARS_PER_TOKEN heuristic; emits PASS/REJECT verdict matching the
 * Python predecessor's compute_budget contract.
 *
 * Exit codes:
 *   0 = PASS (within budget OR no overlay)
 *   1 = REJECT (over budget)
 *   2 = unexpected error (cannot read upstream file)
 *
 * Plan reference: docs/plans/olam-flywheel-cli-namespace/phase-a-tasks.md § A5
 */
import { readFileSync } from 'node:fs';
import { computeBudget, tokensFromText } from '@olam/core/src/lib/k10-budget.js';
export function registerFlywheelK10Measure(parent) {
    parent
        .command('k10-measure')
        .description('Measure K10 token budget for upstream + optional overlay; emit PASS/REJECT verdict')
        .requiredOption('--upstream <path>', 'path to upstream file (e.g. persona prompt)')
        .option('--overlay <path>', 'path to overlay file (omit if none — PASS without enforcement)')
        .option('--json', 'emit verdict as JSON instead of human-readable')
        .action((opts) => {
        let upstreamText;
        try {
            upstreamText = readFileSync(opts.upstream, 'utf8');
        }
        catch (err) {
            process.stderr.write(`[k10-measure-error] cannot read upstream ${opts.upstream}: ${err instanceof Error ? err.message : 'unknown'}\n`);
            process.exit(2);
        }
        const upstreamTokens = tokensFromText(upstreamText);
        let overlayTokens = null;
        if (opts.overlay !== undefined) {
            try {
                const overlayText = readFileSync(opts.overlay, 'utf8');
                overlayTokens = tokensFromText(overlayText);
            }
            catch (err) {
                process.stderr.write(`[k10-measure-error] cannot read overlay ${opts.overlay}: ${err instanceof Error ? err.message : 'unknown'}\n`);
                process.exit(2);
            }
        }
        const result = computeBudget(upstreamTokens, overlayTokens);
        if (opts.json) {
            process.stdout.write(JSON.stringify(result) + '\n');
        }
        else {
            process.stdout.write(`${result.verdict}: ${result.message}\n`);
            process.stdout.write(`  upstream_tokens: ${result.upstream_tokens}\n`);
            if (result.overlay_tokens !== null) {
                process.stdout.write(`  overlay_tokens: ${result.overlay_tokens}\n`);
            }
            if (result.merged !== null) {
                process.stdout.write(`  merged: ${result.merged}\n`);
            }
            process.stdout.write(`  remaining: ${result.remaining} of ${5500} cap\n`);
        }
        process.exit(result.verdict === 'PASS' ? 0 : 1);
    });
}
//# sourceMappingURL=k10-measure.js.map