const STORAGE_KEY = 'additional-tool-preferences';
const STORAGE_VERSION = 1;
/**
 * Determines if a tool's permission can be configured by the user.
 * Tools with needsApproval set by developers are immutable.
 */
export const isToolPermissionConfigurable = (tool) => {
    // If needsApproval is explicitly set (boolean or function), it's immutable
    return tool.needsApproval === undefined;
};
/**
 * Gets the reason why a tool's permission is or isn't configurable
 */
export const getPermissionReason = (tool) => {
    if (tool.needsApproval === true) {
        return 'Always requires approval (set by developer)';
    }
    if (typeof tool.needsApproval === 'function') {
        return 'Conditional approval required (set by developer)';
    }
    return 'User configurable';
};
/**
 * Resolves the effective permission for a tool based on developer settings and user preferences.
 * Priority order:
 * 1. Tool definition needsApproval (immutable) - highest priority
 * 2. User preference (stored) - if tool is configurable
 * 3. Default: 'enabled' - fallback
 */
export const resolveToolPermission = (tool, userPreferences) => {
    // If tool has needsApproval set by developer, it takes priority
    if (tool.needsApproval === true || typeof tool.needsApproval === 'function') {
        return 'ask_first';
    }
    // Otherwise use user preference or default to enabled
    return userPreferences[tool.name] ?? 'enabled';
};
/**
 * Computes the complete permission state for an additional tool
 */
export const computeToolWithPermission = (tool, userPreferences) => {
    const isConfigurable = isToolPermissionConfigurable(tool);
    const effectivePermission = resolveToolPermission(tool, userPreferences);
    const runtimePermission = isConfigurable ? userPreferences[tool.name] : undefined;
    return {
        ...tool,
        runtimePermission,
        effectivePermission,
        isPermissionConfigurable: isConfigurable,
        permissionReason: getPermissionReason(tool)
    };
};
/**
 * Loads additional tool preferences from localStorage
 */
export const loadAdditionalToolPreferences = () => {
    if (typeof window === 'undefined' || !window.localStorage) {
        return {};
    }
    try {
        const stored = window.localStorage.getItem(STORAGE_KEY);
        if (!stored) {
            return {};
        }
        const parsed = JSON.parse(stored);
        // Validate version and structure
        if (parsed.version !== STORAGE_VERSION || typeof parsed.preferences !== 'object') {
            console.warn('Invalid additional tool preferences version, resetting');
            return {};
        }
        return parsed.preferences;
    }
    catch (error) {
        console.warn('Failed to load additional tool preferences:', error);
        return {};
    }
};
/**
 * Saves additional tool preferences to localStorage
 */
export const saveAdditionalToolPreferences = (preferences) => {
    if (typeof window === 'undefined' || !window.localStorage) {
        return;
    }
    try {
        const toStore = {
            version: STORAGE_VERSION,
            preferences,
            lastUpdated: new Date().toISOString()
        };
        window.localStorage.setItem(STORAGE_KEY, JSON.stringify(toStore));
    }
    catch (error) {
        console.warn('Failed to save additional tool preferences:', error);
    }
};
/**
 * Updates a single tool's permission and persists to storage
 */
export const updateToolPermission = (toolName, permission, currentPreferences) => {
    const updated = {
        ...currentPreferences,
        [toolName]: permission
    };
    saveAdditionalToolPreferences(updated);
    return updated;
};
/**
 * Removes a tool's permission override, reverting to default
 */
export const clearToolPermission = (toolName, currentPreferences) => {
    const updated = { ...currentPreferences };
    delete updated[toolName];
    saveAdditionalToolPreferences(updated);
    return updated;
};
/**
 * Processes a list of additional tools, computing their permission states
 */
export const processAdditionalTools = (tools, userPreferences) => {
    const preferences = userPreferences ?? loadAdditionalToolPreferences();
    return tools.map(tool => computeToolWithPermission(tool, preferences));
};
