import { jsx as _jsx } from "react/jsx-runtime";
import React, { createContext, useContext } from 'react';
import { loadAdditionalToolPreferences, processAdditionalTools, updateToolPermission, clearToolPermission, saveAdditionalToolPreferences } from '../lib/additionalToolPermissions';
const AdditionalToolsContext = createContext(undefined);
export const AdditionalToolsProvider = ({ definitions = [], children }) => {
    const [storedDefinitions, setStoredDefinitions] = React.useState(definitions);
    const [preferences, setPreferences] = React.useState({});
    const [isHydrated, setIsHydrated] = React.useState(false);
    React.useEffect(() => {
        setStoredDefinitions(definitions);
    }, [definitions]);
    React.useEffect(() => {
        if (typeof window === 'undefined') {
            return;
        }
        const loaded = loadAdditionalToolPreferences();
        setPreferences(loaded);
        setIsHydrated(true);
    }, []);
    React.useEffect(() => {
        if (!isHydrated) {
            return;
        }
        const toolNames = new Set(storedDefinitions.map((tool) => tool.name));
        setPreferences((previous) => {
            const entries = Object.entries(previous).filter(([toolName]) => toolNames.has(toolName));
            if (entries.length === Object.keys(previous).length) {
                return previous;
            }
            const filtered = entries.reduce((accumulator, [toolName, permission]) => {
                accumulator[toolName] = permission;
                return accumulator;
            }, {});
            saveAdditionalToolPreferences(filtered);
            return filtered;
        });
    }, [isHydrated, storedDefinitions]);
    const processedTools = React.useMemo(() => {
        if (storedDefinitions.length === 0) {
            return [];
        }
        return processAdditionalTools(storedDefinitions, preferences);
    }, [storedDefinitions, preferences]);
    const setToolPermission = React.useCallback((toolName, permission) => {
        setPreferences((previous) => {
            if (permission === 'enabled') {
                const next = clearToolPermission(toolName, previous);
                return next;
            }
            const next = updateToolPermission(toolName, permission, previous);
            return next;
        });
    }, []);
    const resetToolPermission = React.useCallback((toolName) => {
        setPreferences((previous) => clearToolPermission(toolName, previous));
    }, []);
    const contextValue = React.useMemo(() => ({
        definitions: storedDefinitions,
        tools: processedTools,
        preferences,
        isHydrated,
        setToolPermission,
        resetToolPermission
    }), [storedDefinitions, processedTools, preferences, isHydrated, setToolPermission, resetToolPermission]);
    return (_jsx(AdditionalToolsContext.Provider, { value: contextValue, children: children }));
};
export const useAdditionalTools = () => {
    const context = useContext(AdditionalToolsContext);
    if (!context) {
        throw new Error('useAdditionalTools must be used within an AdditionalToolsProvider');
    }
    return context;
};
