"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleContextWatch = handleContextWatch;
exports.handleContextWatchCreate = handleContextWatchCreate;
exports.handleContextWatchPoll = handleContextWatchPoll;
exports.handleContextWatchStop = handleContextWatchStop;
exports.handleContextWatchList = handleContextWatchList;
const validation_js_1 = require("../utils/validation.js");
/**
 * Validates filter parameters for watchers
 */
function validateFilters(filters) {
    if (filters) {
        // Validate keys
        if (filters.keys !== undefined && !Array.isArray(filters.keys)) {
            throw new validation_js_1.ValidationError('keys filter must be an array');
        }
        // Validate channels
        if (filters.channels !== undefined && !Array.isArray(filters.channels)) {
            throw new validation_js_1.ValidationError('channels filter must be an array');
        }
        // Validate categories
        if (filters.categories !== undefined && !Array.isArray(filters.categories)) {
            throw new validation_js_1.ValidationError('categories filter must be an array');
        }
        // Validate category values
        if (filters.categories) {
            const validCategories = ['task', 'decision', 'progress', 'note', 'error', 'warning'];
            for (const cat of filters.categories) {
                if (!validCategories.includes(cat)) {
                    throw new validation_js_1.ValidationError(`Invalid category: ${cat}`);
                }
            }
        }
        // Validate priorities
        if (filters.priorities !== undefined && !Array.isArray(filters.priorities)) {
            throw new validation_js_1.ValidationError('priorities filter must be an array');
        }
        // Validate priority values
        if (filters.priorities) {
            const validPriorities = ['high', 'normal', 'low'];
            for (const priority of filters.priorities) {
                if (!validPriorities.includes(priority)) {
                    throw new validation_js_1.ValidationError(`Invalid priority: ${priority}`);
                }
            }
        }
    }
}
/**
 * context_watch handler - unified handler for all watch operations
 */
async function handleContextWatch(args, repositories, currentSessionId) {
    const { action, watcherId, filters } = args;
    try {
        switch (action) {
            case 'create': {
                // Validate filters
                validateFilters(filters);
                // Create watcher
                const watcher = repositories.watchers.createWatcher({
                    sessionId: currentSessionId,
                    filters: filters || {},
                    ttl: 1800, // 30 minutes default
                });
                return {
                    content: [
                        {
                            type: 'text',
                            text: JSON.stringify({
                                watcherId: watcher.id,
                                created: true,
                                filters: watcher.filters,
                                currentSequence: watcher.lastSequence,
                                expiresIn: '30 minutes',
                            }, null, 2),
                        },
                    ],
                };
            }
            case 'poll': {
                if (!watcherId) {
                    throw new validation_js_1.ValidationError('watcherId is required for poll action');
                }
                // Poll for changes
                const result = repositories.watchers.pollChanges(watcherId, 100);
                if (result.watcherStatus === 'deleted') {
                    throw new validation_js_1.ValidationError(`Watcher not found: ${watcherId}`);
                }
                if (result.watcherStatus === 'expired') {
                    // Check if it's actually stopped vs expired
                    const watcher = repositories.watchers.getWatcher(watcherId);
                    if (watcher && !watcher.isActive) {
                        throw new validation_js_1.ValidationError(`Watcher is stopped: ${watcherId}`);
                    }
                    throw new validation_js_1.ValidationError(`Watcher expired: ${watcherId}`);
                }
                // Transform changes to match test expectations
                const transformedChanges = result.changes.map(change => ({
                    type: change.operation,
                    key: change.key,
                    value: change.operation !== 'DELETE' ? change.newValue : undefined,
                    category: change.category,
                    channel: change.channel,
                    sequence: change.sequenceId,
                    timestamp: change.createdAt,
                }));
                return {
                    content: [
                        {
                            type: 'text',
                            text: JSON.stringify({
                                watcherId,
                                changes: transformedChanges,
                                hasMore: result.hasMore,
                                lastSequence: result.lastSequence,
                                polledAt: new Date().toISOString(),
                            }, null, 2),
                        },
                    ],
                };
            }
            case 'stop': {
                if (!watcherId) {
                    throw new validation_js_1.ValidationError('watcherId is required for stop action');
                }
                const stopped = repositories.watchers.stopWatcher(watcherId);
                if (!stopped) {
                    throw new validation_js_1.ValidationError(`Watcher not found: ${watcherId}`);
                }
                return {
                    content: [
                        {
                            type: 'text',
                            text: JSON.stringify({
                                watcherId,
                                stopped: true,
                            }, null, 2),
                        },
                    ],
                };
            }
            case 'list': {
                const watchers = repositories.watchers.listWatchers(currentSessionId);
                const watcherList = watchers.map(w => ({
                    watcherId: w.id,
                    active: w.isActive,
                    filters: w.filters,
                    lastSequence: w.lastSequence,
                    createdAt: w.createdAt,
                    expiresAt: w.expiresAt,
                }));
                return {
                    content: [
                        {
                            type: 'text',
                            text: JSON.stringify({
                                watchers: watcherList,
                                total: watcherList.length,
                            }, null, 2),
                        },
                    ],
                };
            }
            default:
                throw new validation_js_1.ValidationError(`Unknown action: ${action}`);
        }
    }
    catch (error) {
        if (error instanceof validation_js_1.ValidationError) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `Error: ${error.message}`,
                    },
                ],
            };
        }
        throw error;
    }
}
/**
 * Context watch create handler
 */
async function handleContextWatchCreate(args, repositories, currentSessionId) {
    return handleContextWatch({ ...args, action: 'create' }, repositories, currentSessionId);
}
/**
 * Context watch poll handler
 */
async function handleContextWatchPoll(args, repositories, currentSessionId) {
    return handleContextWatch({ ...args, action: 'poll' }, repositories, currentSessionId);
}
/**
 * Context watch stop handler
 */
async function handleContextWatchStop(args, repositories, currentSessionId) {
    return handleContextWatch({ ...args, action: 'stop' }, repositories, currentSessionId);
}
/**
 * Context watch list handler
 */
async function handleContextWatchList(args, repositories, currentSessionId) {
    return handleContextWatch({ ...args, action: 'list' }, repositories, currentSessionId);
}
