// tools/spec-diff.ts — spec_diff handler
import { compactResult } from './output-formatter.js';
import { hashSnapshotStore, specStore } from '../storage/index.js';
import { buildSpecDiffRows, formatMarkdownDiff } from '../engine/living-spec/index.js';
export async function handleSpecDiff(args) {
    const { projectId, specId } = args;
    // If specId provided, diff that spec; otherwise diff all specs
    const specIds = [];
    if (specId) {
        specIds.push(specId);
    }
    else {
        const allSpecs = await specStore.listSpecs(projectId);
        specIds.push(...allSpecs.map((s) => s.id));
    }
    if (specIds.length === 0) {
        return compactResult('No specs found for this project.');
    }
    const allRows = [];
    for (const sid of specIds) {
        const snapshot = await hashSnapshotStore.getSnapshot(projectId, sid);
        if (!snapshot) {
            continue;
        }
        // Build hash maps from snapshot
        const oldHashes = new Map();
        for (const f of snapshot.files) {
            oldHashes.set(f.file, f.hash);
        }
        // For diff, we compare stored snapshot with current (re-use stored as "old")
        const newHashes = new Map(oldHashes);
        const rows = buildSpecDiffRows(oldHashes, newHashes, sid);
        allRows.push(...rows);
    }
    const markdown = formatMarkdownDiff(allRows);
    const message = allRows.length > 0
        ? `Found ${allRows.length} change(s) across ${specIds.length} spec(s)`
        : `No changes detected across ${specIds.length} spec(s)`;
    const lines = [`**specCount**: ${specIds.length}`, `**diffRows**: ${allRows.length}`, message];
    if (markdown) {
        lines.push('', markdown);
    }
    return compactResult(lines.join('\n'));
}
//# sourceMappingURL=spec-diff.js.map