// tools/drift-watcher-handler.ts — SPEC-388: handlers for watch_spec_drift tool
import { compactResult, formatKeyValue } from './output-formatter.js';
import { hashProjectPath } from '../storage/base-store.js';
import { listSpecs } from '../storage/spec-store.js';
import { startWatcher, stopWatcher, getWatcherState } from '../engine/drift-watcher/index.js';
/**
 * Handle the watch_spec_drift tool invocation.
 * Routes to start/stop/status based on the action field.
 */
export async function handleWatchSpecDrift(input) {
    const { projectPath, specId, action } = input;
    if (action === 'status') {
        const state = getWatcherState(specId);
        if (!state) {
            return compactResult(`No active watcher for spec ${specId}.`);
        }
        return compactResult(formatKeyValue(state, `Watcher: ${specId}`));
    }
    if (action === 'stop') {
        const stopped = stopWatcher(specId);
        return {
            content: [
                {
                    type: 'text',
                    text: stopped
                        ? `Watcher stopped for spec ${specId}.`
                        : `No active watcher found for spec ${specId}.`,
                },
            ],
        };
    }
    // action === 'start'
    const projectId = hashProjectPath(projectPath);
    const specs = await listSpecs(projectId);
    const spec = specs.find((s) => s.id === specId);
    if (!spec) {
        return {
            content: [{ type: 'text', text: `Spec ${specId} not found in project ${projectPath}.` }],
        };
    }
    const specPath = spec.specPath;
    const watchedPaths = [projectPath];
    const state = startWatcher({
        specId,
        projectPath,
        specPath,
        watchedPaths,
    });
    return compactResult(formatKeyValue(state, `Watcher started: ${specId}`));
}
//# sourceMappingURL=drift-watcher-handler.js.map