import { jsx as _jsx } from "react/jsx-runtime";
import { createLLMClient } from '../client-factory.js';
import { ErrorMessage, SuccessMessage } from '../components/message-box.js';
import { reloadAppConfig } from '../config/index.js';
import { loadPreferences, savePreferences, updateLastUsed, updateTitleShape, } from '../config/preferences.js';
import { getToolManager } from '../message-handler.js';
export function useModeHandlers({ client, currentModel, currentProvider, currentTheme: _currentTheme, setClient, setCurrentModel, setCurrentProvider, setCurrentTheme, setMessages, setIsModelSelectionMode, setIsProviderSelectionMode, setIsThemeSelectionMode, setIsTitleShapeSelectionMode, setIsModelDatabaseMode, setIsConfigWizardMode, addToChatQueue, getNextComponentKey, reinitializeMCPServers, }) {
    // Helper function to enter model selection mode
    const enterModelSelectionMode = () => {
        setIsModelSelectionMode(true);
    };
    // Helper function to enter provider selection mode
    const enterProviderSelectionMode = () => {
        setIsProviderSelectionMode(true);
    };
    // Handle model selection
    const handleModelSelect = async (selectedModel) => {
        if (client && selectedModel !== currentModel) {
            client.setModel(selectedModel);
            setCurrentModel(selectedModel);
            // Clear message history when switching models
            setMessages([]);
            await client.clearContext();
            // Update preferences
            updateLastUsed(currentProvider, selectedModel);
            // Add success message to chat queue
            addToChatQueue(_jsx(SuccessMessage, { message: `Model changed to: ${selectedModel}. Chat history cleared.`, hideBox: true }, `model-changed-${getNextComponentKey()}`));
        }
        setIsModelSelectionMode(false);
    };
    // Handle model selection cancel
    const handleModelSelectionCancel = () => {
        setIsModelSelectionMode(false);
    };
    // Handle provider selection
    const handleProviderSelect = async (selectedProvider) => {
        if (selectedProvider !== currentProvider) {
            try {
                // Create new client for the selected provider
                const { client: newClient, actualProvider } = await createLLMClient(selectedProvider);
                // Check if we got the provider we requested
                if (actualProvider !== selectedProvider) {
                    // Provider was forced to a different one (likely due to missing config)
                    addToChatQueue(_jsx(ErrorMessage, { message: `${selectedProvider} is not available. Please ensure it's properly configured in agents.config.json.`, hideBox: true }, `provider-forced-${getNextComponentKey()}`));
                    return; // Don't change anything
                }
                setClient(newClient);
                setCurrentProvider(actualProvider);
                // Set the model from the new client
                const newModel = newClient.getCurrentModel();
                setCurrentModel(newModel);
                // Clear message history when switching providers
                setMessages([]);
                await newClient.clearContext();
                // Update preferences - use the actualProvider (which is what was successfully created)
                updateLastUsed(actualProvider, newModel);
                // Add success message to chat queue
                addToChatQueue(_jsx(SuccessMessage, { message: `Provider changed to: ${actualProvider}, model: ${newModel}. Chat history cleared.`, hideBox: true }, `provider-changed-${getNextComponentKey()}`));
            }
            catch (error) {
                // Add error message if provider change fails
                addToChatQueue(_jsx(ErrorMessage, { message: `Failed to change provider to ${selectedProvider}: ${String(error)}`, hideBox: true }, `provider-error-${getNextComponentKey()}`));
            }
        }
        setIsProviderSelectionMode(false);
    };
    // Handle provider selection cancel
    const handleProviderSelectionCancel = () => {
        setIsProviderSelectionMode(false);
    };
    // Helper function to enter theme selection mode
    const enterThemeSelectionMode = () => {
        setIsThemeSelectionMode(true);
    };
    // Helper function to enter title shape selection mode
    const enterTitleShapeSelectionMode = () => {
        setIsTitleShapeSelectionMode(true);
    };
    // Handle title shape selection
    const handleTitleShapeSelect = (selectedShape) => {
        updateTitleShape(selectedShape);
        // Add success message to chat queue
        addToChatQueue(_jsx(SuccessMessage, { message: `Title shape changed to: ${selectedShape}.`, hideBox: true }, `title-shape-changed-${getNextComponentKey()}`));
        setIsTitleShapeSelectionMode(false);
    };
    // Handle title shape selection cancel
    const handleTitleShapeSelectionCancel = () => {
        setIsTitleShapeSelectionMode(false);
    };
    // Handle theme selection
    const handleThemeSelect = (selectedTheme) => {
        const preferences = loadPreferences();
        preferences.selectedTheme = selectedTheme;
        savePreferences(preferences);
        // Update the theme state immediately for real-time switching
        setCurrentTheme(selectedTheme);
        // Add success message to chat queue
        addToChatQueue(_jsx(SuccessMessage, { message: `Theme changed to: ${selectedTheme}.`, hideBox: true }, `theme-changed-${getNextComponentKey()}`));
        setIsThemeSelectionMode(false);
    };
    // Handle theme selection cancel
    const handleThemeSelectionCancel = () => {
        setIsThemeSelectionMode(false);
    };
    // Helper function to enter model database mode
    const enterModelDatabaseMode = () => {
        setIsModelDatabaseMode(true);
    };
    // Handle model database cancel
    const handleModelDatabaseCancel = () => {
        setIsModelDatabaseMode(false);
    };
    // Helper function to enter config wizard mode
    const enterConfigWizardMode = () => {
        setIsConfigWizardMode(true);
    };
    // Handle config wizard cancel/complete
    const handleConfigWizardComplete = async (configPath) => {
        setIsConfigWizardMode(false);
        if (configPath) {
            addToChatQueue(_jsx(SuccessMessage, { message: `Configuration saved to: ${configPath}.`, hideBox: true }, `config-wizard-complete-${getNextComponentKey()}`));
            // Reload the app configuration to pick up the newly saved config
            reloadAppConfig();
            // Reinitialize client with new configuration
            try {
                const preferences = loadPreferences();
                const { client: newClient, actualProvider } = await createLLMClient(preferences.lastProvider);
                setClient(newClient);
                setCurrentProvider(actualProvider);
                const newModel = newClient.getCurrentModel();
                setCurrentModel(newModel);
                // Clear message history when switching providers
                setMessages([]);
                await newClient.clearContext();
                // Reinitialize MCP servers with the new configuration
                const toolManager = getToolManager();
                if (toolManager) {
                    try {
                        await reinitializeMCPServers(toolManager);
                        addToChatQueue(_jsx(SuccessMessage, { message: "MCP servers reinitialized with new configuration.", hideBox: true }, `mcp-reinit-${getNextComponentKey()}`));
                    }
                    catch (mcpError) {
                        addToChatQueue(_jsx(ErrorMessage, { message: `Failed to reinitialize MCP servers: ${String(mcpError)}`, hideBox: true }, `mcp-reinit-error-${getNextComponentKey()}`));
                    }
                }
                addToChatQueue(_jsx(SuccessMessage, { message: `Ready! Using provider: ${actualProvider}, model: ${newModel}`, hideBox: true }, `config-init-${getNextComponentKey()}`));
            }
            catch (error) {
                addToChatQueue(_jsx(ErrorMessage, { message: `Failed to initialize with new configuration: ${String(error)}`, hideBox: true }, `config-init-error-${getNextComponentKey()}`));
            }
        }
    };
    const handleConfigWizardCancel = () => {
        setIsConfigWizardMode(false);
    };
    return {
        enterModelSelectionMode,
        enterProviderSelectionMode,
        enterThemeSelectionMode,
        enterTitleShapeSelectionMode,
        handleTitleShapeSelect,
        handleTitleShapeSelectionCancel,
        enterModelDatabaseMode,
        enterConfigWizardMode,
        handleModelSelect,
        handleModelSelectionCancel,
        handleProviderSelect,
        handleProviderSelectionCancel,
        handleThemeSelect,
        handleThemeSelectionCancel,
        handleModelDatabaseCancel,
        handleConfigWizardComplete,
        handleConfigWizardCancel,
    };
}
//# sourceMappingURL=useModeHandlers.js.map