// tools/configure-workers-handler.ts — Handler for configure_workers tool (SPEC-078 AC-12)
import { getWorkerEngine } from '../engine/workers/index.js';
import { updateOverride } from '../storage/worker-store.js';
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleConfigureWorkers(args) {
    const engine = getWorkerEngine();
    const definitions = engine.getWorkerDefinitions();
    const worker = definitions.find((w) => w.name === args.workerName);
    if (!worker) {
        const available = definitions.map((w) => w.name).join(', ');
        return {
            content: [
                {
                    type: 'text',
                    text: `Worker "${args.workerName}" not found. Available: ${available}`,
                },
            ],
            isError: true,
        };
    }
    const override = {};
    let description = '';
    switch (args.action) {
        case 'enable':
            override.enabled = true;
            description = `Enabled worker "${args.workerName}"`;
            break;
        case 'disable':
            override.enabled = false;
            description = `Disabled worker "${args.workerName}"`;
            break;
        case 'set-cooldown': {
            if (typeof args.value !== 'number' || args.value < 0) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: 'set-cooldown requires a non-negative number value (milliseconds).',
                        },
                    ],
                    isError: true,
                };
            }
            override.cooldownMs = args.value;
            description = `Set cooldown for "${args.workerName}" to ${String(args.value)}ms`;
            break;
        }
        case 'set-priority': {
            if (typeof args.value !== 'number' || args.value < 1 || args.value > 10) {
                return {
                    content: [{ type: 'text', text: 'set-priority requires a number between 1 and 10.' }],
                    isError: true,
                };
            }
            override.priority = args.value;
            description = `Set priority for "${args.workerName}" to ${String(args.value)}`;
            break;
        }
        case 'add-pattern': {
            if (typeof args.value !== 'string' || args.value.length === 0) {
                return {
                    content: [{ type: 'text', text: 'add-pattern requires a string value (glob pattern).' }],
                    isError: true,
                };
            }
            const currentPatterns = worker.trigger.patterns ?? [];
            if (currentPatterns.includes(args.value)) {
                return {
                    content: [
                        {
                            type: 'text',
                            text: `Pattern "${args.value}" already exists for "${args.workerName}".`,
                        },
                    ],
                };
            }
            override.triggerPatterns = [...currentPatterns, args.value];
            description = `Added pattern "${args.value}" to "${args.workerName}"`;
            break;
        }
        case 'remove-pattern': {
            if (typeof args.value !== 'string' || args.value.length === 0) {
                return {
                    content: [
                        { type: 'text', text: 'remove-pattern requires a string value (glob pattern).' },
                    ],
                    isError: true,
                };
            }
            const patterns = worker.trigger.patterns ?? [];
            if (!patterns.includes(args.value)) {
                return {
                    content: [
                        { type: 'text', text: `Pattern "${args.value}" not found in "${args.workerName}".` },
                    ],
                    isError: true,
                };
            }
            override.triggerPatterns = patterns.filter((p) => p !== args.value);
            description = `Removed pattern "${args.value}" from "${args.workerName}"`;
            break;
        }
    }
    // Apply to engine
    engine.applyOverride(args.workerName, override);
    // Persist to storage
    await updateOverride(args.projectId, args.workerName, override);
    return {
        content: [
            { type: 'text', text: `${description}.\nConfiguration saved for project ${args.projectId}.` },
        ],
    };
}
//# sourceMappingURL=configure-workers-handler.js.map