// tools/living-spec-watch.ts — living_spec_watch and living_spec_status handlers
import { createDriftWatcher } from '../engine/living-spec/index.js';
// Global watcher registry: one watcher per project
const watchers = new Map();
/**
 * Get or create a drift watcher for a project.
 */
function getWatcher(input) {
    let watcher = watchers.get(input.projectId);
    if (!watcher) {
        watcher = createDriftWatcher({
            projectId: input.projectId,
            ttlSeconds: input.ttlSeconds,
            excludePatterns: input.excludePatterns,
        });
        watchers.set(input.projectId, watcher);
    }
    return watcher;
}
export function handleLivingSpecWatch(args) {
    const { projectId, action } = args;
    if (action === 'start') {
        const watcher = getWatcher(args);
        watcher.start();
        return Promise.resolve({
            content: [
                {
                    type: 'text',
                    text: JSON.stringify({
                        message: `Drift watcher started for project ${projectId}`,
                        status: watcher.status(),
                    }, null, 2),
                },
            ],
        });
    }
    if (action === 'stop') {
        const watcher = watchers.get(projectId);
        if (!watcher) {
            return Promise.resolve({
                content: [
                    {
                        type: 'text',
                        text: JSON.stringify({ message: `No watcher running for project ${projectId}` }, null, 2),
                    },
                ],
            });
        }
        watcher.stop();
        watchers.delete(projectId);
        return Promise.resolve({
            content: [
                {
                    type: 'text',
                    text: JSON.stringify({ message: `Drift watcher stopped for project ${projectId}` }, null, 2),
                },
            ],
        });
    }
    // status
    const watcher = watchers.get(projectId);
    const status = watcher
        ? watcher.status()
        : { running: false, startedAt: null, cacheSize: 0, projectId };
    return Promise.resolve({
        content: [
            {
                type: 'text',
                text: JSON.stringify({ status }, null, 2),
            },
        ],
    });
}
export function handleLivingSpecStatus(args) {
    const { projectId } = args;
    const watcher = watchers.get(projectId);
    const status = watcher
        ? watcher.status()
        : { running: false, startedAt: null, cacheSize: 0, projectId };
    return Promise.resolve({
        content: [
            {
                type: 'text',
                text: JSON.stringify({
                    watcherStatus: status,
                    message: status.running
                        ? `Watcher active since ${status.startedAt}, ${status.cacheSize} cached entries`
                        : 'No active watcher for this project',
                }, null, 2),
            },
        ],
    });
}
//# sourceMappingURL=living-spec-watch.js.map