/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useCallback, useState } from 'react';
import { MessageType } from '../types.js';
import { useAppDispatch } from '../contexts/AppDispatchContext.js';
export const useToolsDialog = ({ addMessage, appState, config, }) => {
    const appDispatch = useAppDispatch();
    const showDialog = appState.openDialogs.tools;
    const [action, setAction] = useState('disable');
    const [availableTools, setAvailableTools] = useState([]);
    const [disabledTools, setDisabledTools] = useState([]);
    const openDialog = useCallback(async (dialogAction) => {
        try {
            const toolRegistry = await config.getToolRegistry();
            if (!toolRegistry) {
                addMessage({
                    type: MessageType.ERROR,
                    content: 'Could not retrieve tool registry.',
                    timestamp: new Date(),
                });
                return;
            }
            // Get current disabled tools from ephemeral settings
            const ephemeralSettings = config.getEphemeralSettings() || {};
            const currentDisabledTools = ephemeralSettings['disabled-tools'] || [];
            // Get all non-MCP tools
            const allTools = toolRegistry.getAllTools();
            const geminiTools = allTools.filter((tool) => !('serverName' in tool));
            // Filter tools based on the action
            let tools;
            if (dialogAction === 'disable') {
                // Show only enabled tools for disabling
                tools = geminiTools.filter((tool) => !currentDisabledTools.includes(tool.name));
            }
            else {
                // Show only disabled tools for enabling
                tools = geminiTools.filter((tool) => currentDisabledTools.includes(tool.name));
            }
            if (tools.length === 0) {
                addMessage({
                    type: MessageType.INFO,
                    content: dialogAction === 'disable'
                        ? 'All tools are already disabled.'
                        : 'No tools are currently disabled.',
                    timestamp: new Date(),
                });
                return;
            }
            setAction(dialogAction);
            setAvailableTools(tools);
            setDisabledTools(currentDisabledTools);
            appDispatch({ type: 'OPEN_DIALOG', payload: 'tools' });
        }
        catch (e) {
            addMessage({
                type: MessageType.ERROR,
                content: `Failed to load tools: ${e instanceof Error ? e.message : String(e)}`,
                timestamp: new Date(),
            });
        }
    }, [addMessage, appDispatch, config]);
    const closeDialog = useCallback(() => appDispatch({ type: 'CLOSE_DIALOG', payload: 'tools' }), [appDispatch]);
    const handleSelect = useCallback((toolName) => {
        const selectedTool = availableTools.find((t) => t.name === toolName);
        if (!selectedTool)
            return;
        // Update disabled tools list
        let updatedDisabledTools;
        if (action === 'disable') {
            updatedDisabledTools = [...disabledTools, toolName];
        }
        else {
            updatedDisabledTools = disabledTools.filter((name) => name !== toolName);
        }
        // Update ephemeral settings
        config.setEphemeralSetting('disabled-tools', updatedDisabledTools);
        addMessage({
            type: MessageType.INFO,
            content: `Tool '${selectedTool.displayName}' has been ${action}d.`,
            timestamp: new Date(),
        });
        appDispatch({ type: 'CLOSE_DIALOG', payload: 'tools' });
    }, [addMessage, appDispatch, config, action, availableTools, disabledTools]);
    return {
        showDialog,
        openDialog,
        closeDialog,
        action,
        availableTools,
        disabledTools,
        handleSelect,
    };
};
//# sourceMappingURL=useToolsDialog.js.map