// tools/configure-llm-providers-handler.ts — Handler for configure_llm_providers tool.
// SPEC-083 AC-08
import { t, ti } from '../i18n/index.js';
import providersData from '../config/llm-providers.json' with { type: 'json' };
/** In-memory config state. In production, persisted to data/global/llm-provider-config.json. */
let currentConfig = {
    providers: providersData.providers.map((p) => p.id),
    failoverOrder: providersData.providers.map((p) => p.id),
    strategy: 'cost-based',
    customProviders: [],
};
/**
 * Get the current LLM provider configuration.
 */
export function getCurrentConfig() {
    return { ...currentConfig };
}
/**
 * Get all provider definitions (built-in + custom).
 */
export function getAllProviderDefinitions() {
    const builtIn = providersData.providers;
    return [...builtIn, ...currentConfig.customProviders];
}
/**
 * Handle the configure_llm_providers tool call.
 */
export function handleConfigureLLMProviders(args) {
    switch (args.action) {
        case 'add':
            return handleAdd(args);
        case 'remove':
            return handleRemove(args);
        case 'update':
            return handleUpdate(args);
        case 'set_priority':
            return handleSetPriority(args);
        case 'set_strategy':
            return handleSetStrategy(args);
    }
}
function handleAdd(args) {
    if (!args.provider) {
        return errorResult(ti('tools.configure_llm_providers.providerRequired', { action: 'add' }));
    }
    const existing = getAllProviderDefinitions().find((p) => p.id === args.provider?.id);
    if (existing) {
        return errorResult(ti('tools.configure_llm_providers.alreadyExists', { id: String(args.provider.id) }));
    }
    currentConfig.customProviders.push(args.provider);
    currentConfig.providers.push(args.provider.id);
    currentConfig.failoverOrder.push(args.provider.id);
    return successResult(ti('tools.configure_llm_providers.addSuccess', { name: args.provider.name }));
}
function handleRemove(args) {
    if (!args.providerId) {
        return errorResult(t('tools.configure_llm_providers.providerIdRequired'));
    }
    const remaining = currentConfig.providers.filter((p) => p !== args.providerId);
    if (remaining.length === 0) {
        return errorResult(t('tools.configure_llm_providers.cannotRemoveLast'));
    }
    currentConfig.providers = remaining;
    currentConfig.failoverOrder = currentConfig.failoverOrder.filter((p) => p !== args.providerId);
    currentConfig.customProviders = currentConfig.customProviders.filter((p) => p.id !== args.providerId);
    return successResult(ti('tools.configure_llm_providers.removeSuccess', { id: args.providerId }));
}
function handleUpdate(args) {
    if (!args.provider) {
        return errorResult(ti('tools.configure_llm_providers.providerRequired', { action: 'update' }));
    }
    const idx = currentConfig.customProviders.findIndex((p) => p.id === args.provider?.id);
    if (idx >= 0) {
        currentConfig.customProviders[idx] = args.provider;
    }
    else {
        // If updating a built-in, add as custom override
        currentConfig.customProviders.push(args.provider);
    }
    return successResult(ti('tools.configure_llm_providers.updateSuccess', { name: args.provider.name }));
}
function handleSetPriority(args) {
    if (!args.failoverOrder || args.failoverOrder.length === 0) {
        return errorResult(t('tools.configure_llm_providers.failoverRequired'));
    }
    currentConfig.failoverOrder = args.failoverOrder;
    return successResult(ti('tools.configure_llm_providers.priorityUpdated', { order: args.failoverOrder.join(' > ') }));
}
function handleSetStrategy(args) {
    if (!args.strategy) {
        return errorResult(t('tools.configure_llm_providers.strategyRequired'));
    }
    currentConfig.strategy = args.strategy;
    return successResult(ti('tools.configure_llm_providers.strategySet', { strategy: args.strategy }));
}
function successResult(message) {
    const lines = [
        t('tools.configure_llm_providers.heading'),
        '',
        message,
        '',
        ti('tools.configure_llm_providers.activeProviders', {
            providers: currentConfig.providers.join(', '),
        }),
        ti('tools.configure_llm_providers.failoverOrder', {
            order: currentConfig.failoverOrder.join(' > '),
        }),
        ti('tools.configure_llm_providers.strategy', { strategy: currentConfig.strategy }),
    ];
    if (currentConfig.customProviders.length > 0) {
        lines.push(ti('tools.configure_llm_providers.customProviders', {
            names: currentConfig.customProviders.map((p) => p.name).join(', '),
        }));
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
function errorResult(message) {
    return {
        content: [{ type: 'text', text: ti('tools.configure_llm_providers.errorPrefix', { message }) }],
        isError: true,
    };
}
/**
 * Reset config to defaults (for testing).
 */
export function resetConfig() {
    currentConfig = {
        providers: providersData.providers.map((p) => p.id),
        failoverOrder: providersData.providers.map((p) => p.id),
        strategy: 'cost-based',
        customProviders: [],
    };
}
//# sourceMappingURL=configure-llm-providers-handler.js.map