import { splitModelRegistryKey, withRegistryKey } from './registry-helpers.js';
export function getProviderLookupCandidates(providerName, aliases) {
    const candidates = new Set([providerName]);
    const directAlias = aliases[providerName];
    if (directAlias)
        candidates.add(directAlias);
    for (const [catalogName, registeredName] of Object.entries(aliases)) {
        if (registeredName === providerName)
            candidates.add(catalogName);
        if (directAlias && registeredName === directAlias)
            candidates.add(catalogName);
    }
    return [...candidates];
}
export function findModelDefinition(modelId, registry) {
    if (!modelId.includes(':'))
        return undefined;
    return registry.find((model) => model.registryKey === modelId);
}
export function findModelDefinitionForProvider(modelId, providerName, registry, aliases) {
    const providerCandidates = new Set(getProviderLookupCandidates(providerName, aliases));
    const resolvedModelId = modelId.includes(':') ? splitModelRegistryKey(modelId).resolvedModelId : modelId;
    if (modelId.includes(':')) {
        const registryMatch = findModelDefinition(modelId, registry);
        return registryMatch && providerCandidates.has(registryMatch.provider)
            ? registryMatch
            : undefined;
    }
    return registry.find((model) => model.id === resolvedModelId && providerCandidates.has(model.provider));
}
export function buildModelRegistry(input) {
    const customModels = input.customModels.map(withRegistryKey);
    const runtimeModels = input.runtimeModels.map(withRegistryKey);
    const syntheticModels = input.syntheticModels.map(withRegistryKey);
    const catalogModels = input.catalogModels.map(withRegistryKey);
    const discoveredModels = input.discoveredModels.map(withRegistryKey);
    const sameRegistryKey = (left, right) => left.registryKey === right.registryKey;
    const catalogFiltered = catalogModels.filter((builtin) => !customModels.some((model) => sameRegistryKey(model, builtin)) &&
        !runtimeModels.some((model) => sameRegistryKey(model, builtin)) &&
        !input.suppressedCatalogRegistryKeys.has(builtin.registryKey) &&
        !input.suppressedCatalogRegistryKeys.has(`${builtin.provider}:${builtin.id}`) &&
        !builtin.id.startsWith('hf:'));
    const discoveredFiltered = discoveredModels.filter((discovered) => !catalogModels.some((model) => sameRegistryKey(model, discovered)) &&
        !customModels.some((model) => sameRegistryKey(model, discovered)) &&
        !runtimeModels.some((model) => sameRegistryKey(model, discovered)));
    return [
        ...customModels,
        ...runtimeModels,
        ...syntheticModels,
        ...catalogFiltered,
        ...discoveredFiltered,
    ];
}
