import { loadHooksConfig, saveHooksConfig, getDefaultHooksConfig, } from '../engine/filesystem-watcher/index.js';
/**
 * Handles the configure_filesystem_hooks and filesystem_hooks_status tool calls.
 */
export async function handleConfigureFilesystemHooks(input) {
    const { action, projectPath } = input;
    if (action === 'enable') {
        return handleEnable(projectPath);
    }
    if (action === 'disable') {
        return handleDisable(projectPath);
    }
    if (action === 'add-hook') {
        return handleAddHook(input);
    }
    if (action === 'remove-hook') {
        return handleRemoveHook(input);
    }
    if (action === 'list') {
        return handleList(projectPath);
    }
    return handleStatus(projectPath);
}
async function handleEnable(projectPath) {
    const config = await loadHooksConfig(projectPath);
    const updated = { ...config, enabled: true };
    await saveHooksConfig(projectPath, updated);
    return { content: [{ type: 'text', text: 'Filesystem hooks enabled.' }] };
}
async function handleDisable(projectPath) {
    const config = await loadHooksConfig(projectPath);
    const updated = { ...config, enabled: false };
    await saveHooksConfig(projectPath, updated);
    return { content: [{ type: 'text', text: 'Filesystem hooks disabled.' }] };
}
async function handleAddHook(input) {
    if (!input.hook) {
        return {
            content: [{ type: 'text', text: 'Error: hook is required for add-hook action.' }],
            isError: true,
        };
    }
    const config = await loadHooksConfig(input.projectPath);
    const updated = { ...config, hooks: [...config.hooks, input.hook] };
    await saveHooksConfig(input.projectPath, updated);
    const { event, pattern, tool } = input.hook;
    return {
        content: [{ type: 'text', text: `Hook added: ${event} on ${pattern} → ${tool}` }],
    };
}
async function handleRemoveHook(input) {
    if (input.hookIndex === undefined) {
        return {
            content: [{ type: 'text', text: 'Error: hookIndex is required for remove-hook action.' }],
            isError: true,
        };
    }
    const config = await loadHooksConfig(input.projectPath);
    if (input.hookIndex < 0 || input.hookIndex >= config.hooks.length) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Error: hookIndex ${input.hookIndex} out of bounds (${config.hooks.length} hooks).`,
                },
            ],
            isError: true,
        };
    }
    const hooks = [...config.hooks];
    hooks.splice(input.hookIndex, 1);
    await saveHooksConfig(input.projectPath, { ...config, hooks });
    return { content: [{ type: 'text', text: 'Hook removed.' }] };
}
async function handleList(projectPath) {
    const config = await loadHooksConfig(projectPath);
    if (config.hooks.length === 0) {
        return { content: [{ type: 'text', text: 'No filesystem hooks configured.' }] };
    }
    const lines = config.hooks.map((h, i) => {
        const debounce = h.debounceMs !== undefined ? ` (debounce: ${h.debounceMs}ms)` : '';
        return `[${i}] ${h.event} on "${h.pattern}" → ${h.tool}${debounce}`;
    });
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
async function handleStatus(projectPath) {
    const config = await loadHooksConfig(projectPath);
    const defaulted = config.hooks.length === 0 && !config.enabled;
    const configSource = defaulted ? ' (default — no hooks.json found)' : '';
    const status = config.enabled ? 'enabled' : 'disabled';
    const text = `Filesystem hooks: ${status}${configSource}\n` + `Configured hooks: ${config.hooks.length}`;
    return { content: [{ type: 'text', text }] };
}
// Re-export for tests
export { getDefaultHooksConfig };
//# sourceMappingURL=filesystem-hooks-handler.js.map