// tools/spec-diff.ts — spec_diff handler
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 {
            content: [
                {
                    type: 'text',
                    text: JSON.stringify({ message: 'No specs found for this project' }, null, 2),
                },
            ],
        };
    }
    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);
    return {
        content: [
            {
                type: 'text',
                text: JSON.stringify({
                    specCount: specIds.length,
                    diffRows: allRows.length,
                    markdown,
                    message: allRows.length > 0
                        ? `Found ${allRows.length} change(s) across ${specIds.length} spec(s)`
                        : `No changes detected across ${specIds.length} spec(s)`,
                }, null, 2),
            },
        ],
    };
}
//# sourceMappingURL=spec-diff.js.map