import { VirtualRouterError, VirtualRouterErrorCode } from './types.js';
export class ProviderRegistry {
    providers = new Map();
    constructor(profiles) {
        if (profiles) {
            this.load(profiles);
        }
    }
    load(profiles) {
        this.providers.clear();
        for (const [key, profile] of Object.entries(profiles)) {
            const normalized = ProviderRegistry.normalizeProfile(key, profile);
            this.providers.set(normalized.providerKey, normalized);
        }
    }
    get(providerKey) {
        const profile = this.providers.get(providerKey);
        if (!profile) {
            throw new VirtualRouterError(`Provider ${providerKey} is not registered`, VirtualRouterErrorCode.CONFIG_ERROR, {
                providerKey
            });
        }
        return profile;
    }
    has(providerKey) {
        return this.providers.has(providerKey);
    }
    listKeys() {
        return Array.from(this.providers.keys());
    }
    resolveRuntimeKeyByAlias(providerId, keyAlias) {
        const pattern = new RegExp(`^${providerId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.${keyAlias.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?:\\.|$)`);
        for (const key of this.providers.keys()) {
            if (pattern.test(key)) {
                return key;
            }
        }
        return null;
    }
    resolveRuntimeKeyByIndex(providerId, keyIndex) {
        const index = keyIndex - 1;
        if (index < 0)
            return null;
        const keys = this.listProviderKeys(providerId);
        if (index >= keys.length)
            return null;
        return keys[index];
    }
    listProviderKeys(providerId) {
        const pattern = new RegExp(`^${providerId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.`);
        return this.listKeys().filter(key => pattern.test(key));
    }
    resolveRuntimeKeyByModel(providerId, modelId) {
        if (!providerId || !modelId) {
            return null;
        }
        const normalizedModel = modelId.trim();
        if (!normalizedModel) {
            return null;
        }
        const providerKeys = this.listProviderKeys(providerId);
        for (const key of providerKeys) {
            const profile = this.providers.get(key);
            const candidate = profile?.modelId ?? deriveModelId(key);
            if (candidate === normalizedModel) {
                return key;
            }
        }
        return null;
    }
    buildTarget(providerKey) {
        const profile = this.get(providerKey);
        const modelId = profile.modelId ?? deriveModelId(profile.providerKey);
        if (!modelId) {
            throw new VirtualRouterError(`Provider ${providerKey} is missing model identifier`, VirtualRouterErrorCode.CONFIG_ERROR, { providerKey });
        }
        return {
            providerKey: profile.providerKey,
            providerType: profile.providerType,
            outboundProfile: profile.outboundProfile,
            compatibilityProfile: profile.compatibilityProfile,
            runtimeKey: profile.runtimeKey,
            modelId,
            processMode: profile.processMode || 'chat',
            responsesConfig: profile.responsesConfig,
            streaming: profile.streaming,
            maxOutputTokens: profile.maxOutputTokens,
            maxContextTokens: profile.maxContextTokens,
            ...(profile.deepseek ? { deepseek: profile.deepseek } : {})
        };
    }
    static normalizeProfile(key, profile) {
        const providerKey = profile.providerKey ?? key;
        const modelId = profile.modelId ?? deriveModelId(providerKey);
        return {
            providerKey,
            providerType: profile.providerType,
            endpoint: profile.endpoint,
            auth: profile.auth,
            ...(profile.enabled !== undefined ? { enabled: profile.enabled } : {}),
            outboundProfile: profile.outboundProfile,
            compatibilityProfile: profile.compatibilityProfile,
            runtimeKey: profile.runtimeKey,
            modelId,
            processMode: profile.processMode || 'chat',
            responsesConfig: profile.responsesConfig,
            streaming: profile.streaming,
            maxOutputTokens: profile.maxOutputTokens,
            maxContextTokens: profile.maxContextTokens,
            ...(profile.deepseek ? { deepseek: profile.deepseek } : {}),
            ...(profile.serverToolsDisabled ? { serverToolsDisabled: true } : {})
        };
    }
}
function deriveModelId(providerKey) {
    if (!providerKey)
        return undefined;
    const firstDot = providerKey.indexOf('.');
    if (firstDot <= 0 || firstDot === providerKey.length - 1)
        return undefined;
    const remainder = providerKey.slice(firstDot + 1);
    const secondDot = remainder.indexOf('.');
    if (secondDot <= 0 || secondDot === remainder.length - 1)
        return remainder.trim() || undefined;
    return remainder.slice(secondDot + 1).trim() || undefined;
}
