import { getSecretValue, listSecretDefinitions } from '../core/secretStore.js';
import { getToolManifest } from './toolManifest.js';
const SECRET_LOOKUP = buildSecretLookup();
const TOOL_OPTIONS = buildToolToggleOptions();
const PLUGIN_TO_OPTION = buildPluginLookup();
const LOCKED_OPTION_IDS = buildLockedOptionIds();
const DEFAULT_OPTION_IDS = buildDefaultOptionIds();
export function getToolToggleOptions() {
    return [...TOOL_OPTIONS];
}
export function buildEnabledToolSet(saved, _profile) {
    const enabled = new Set();
    // Always include locked/default core packages to keep the AI flow intact
    for (const id of LOCKED_OPTION_IDS) {
        enabled.add(id);
    }
    if (!saved) {
        for (const id of DEFAULT_OPTION_IDS) {
            enabled.add(id);
        }
        return enabled;
    }
    const knownIds = new Set(TOOL_OPTIONS.map((option) => option.id));
    for (const id of saved.enabledTools ?? []) {
        if (knownIds.has(id)) {
            enabled.add(id);
        }
    }
    // Enforce locked/default packages even if missing from saved settings
    for (const id of LOCKED_OPTION_IDS) {
        enabled.add(id);
    }
    for (const id of DEFAULT_OPTION_IDS) {
        enabled.add(id);
    }
    return enabled;
}
export function evaluateToolPermissions(selection) {
    const allowedPluginIds = new Set();
    const warnings = [];
    for (const option of TOOL_OPTIONS) {
        if (!selection.has(option.id)) {
            continue;
        }
        if (option.requiresSecret) {
            const secret = getSecretValue(option.requiresSecret);
            if (!secret) {
                warnings.push({
                    id: option.id,
                    label: option.label,
                    reason: 'missing-secret',
                    secretId: option.requiresSecret,
                });
                continue;
            }
        }
        for (const pluginId of option.pluginIds) {
            allowedPluginIds.add(pluginId);
        }
    }
    return {
        allowedPluginIds,
        warnings,
    };
}
export function isPluginEnabled(pluginId, allowedPluginIds) {
    const associated = PLUGIN_TO_OPTION.get(pluginId);
    if (!associated) {
        return true;
    }
    return allowedPluginIds.has(pluginId);
}
function buildPluginLookup() {
    const map = new Map();
    for (const option of TOOL_OPTIONS) {
        for (const pluginId of option.pluginIds) {
            map.set(pluginId, option);
        }
    }
    return map;
}
function buildToolToggleOptions() {
    const manifest = getToolManifest();
    return manifest.options.map((option) => normalizeToggleOption(option));
}
function buildLockedOptionIds() {
    const ids = new Set();
    for (const option of TOOL_OPTIONS) {
        if (option.locked) {
            ids.add(option.id);
        }
    }
    return ids;
}
function buildDefaultOptionIds() {
    const ids = new Set();
    for (const option of TOOL_OPTIONS) {
        if (option.defaultEnabled) {
            ids.add(option.id);
        }
    }
    return ids;
}
function normalizeToggleOption(option) {
    const pluginIds = option.pluginIds ?? [];
    if (!pluginIds.length) {
        throw new Error(`Tool option "${option.id}" is missing plugin bindings.`);
    }
    const normalized = {
        id: option.id,
        label: option.label,
        description: option.description,
        defaultEnabled: option.defaultEnabled,
        pluginIds: [...pluginIds],
    };
    if (option.category) {
        normalized.category = option.category;
    }
    if (option.scopes?.length) {
        normalized.scopes = [...option.scopes];
    }
    const secret = normalizeSecret(option.requiresSecret);
    if (secret) {
        normalized.requiresSecret = secret;
    }
    if (option.metadata) {
        normalized.metadata = { ...option.metadata };
    }
    if (option.metadata && option.metadata['locked'] === true) {
        normalized.locked = true;
    }
    if (option.metadata && option.metadata['restartRequired'] === true) {
        normalized.restartRequired = true;
    }
    return normalized;
}
function buildSecretLookup() {
    const ids = listSecretDefinitions().map((secret) => secret.id);
    return new Set(ids);
}
function normalizeSecret(value) {
    if (!value) {
        return undefined;
    }
    if (!SECRET_LOOKUP.has(value)) {
        throw new Error(`Tool manifest references unknown secret "${value}".`);
    }
    return value;
}
//# sourceMappingURL=toolRegistry.js.map