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);
const EMPTY_DEFINITIONS = [];
export const AdditionalToolsProvider = ({ definitions, children }) => {
    const resolvedDefinitions = definitions ?? EMPTY_DEFINITIONS;
    const [preferences, setPreferences] = React.useState({});
    const [isHydrated, setIsHydrated] = React.useState(false);
    React.useEffect(() => {
        if (typeof window === 'undefined') {
            return;
        }
        const loaded = loadAdditionalToolPreferences();
        setPreferences(loaded);
        setIsHydrated(true);
    }, []);
    React.useEffect(() => {
        if (!isHydrated) {
            return;
        }
        const toolNames = new Set(resolvedDefinitions.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, resolvedDefinitions]);
    const processedTools = React.useMemo(() => {
        if (resolvedDefinitions.length === 0) {
            return [];
        }
        return processAdditionalTools(resolvedDefinitions, preferences);
    }, [resolvedDefinitions, 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: resolvedDefinitions,
        tools: processedTools,
        preferences,
        isHydrated,
        setToolPermission,
        resetToolPermission
    }), [resolvedDefinitions, 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;
};
