function normalizeCapabilityAlias(value) {
    if (value === 'chat')
        return 'text.generate';
    if (value === 'embedding')
        return 'text.embed';
    if (value === 'image')
        return 'image.generate';
    if (value === 'video')
        return 'video.generate';
    if (value === 'world')
        return 'world.generate';
    if (value === 'tts')
        return 'audio.synthesize';
    if (value === 'stt' || value === 'speech.transcribe')
        return 'audio.transcribe';
    if (value === 'music')
        return 'music.generate';
    return null;
}
function isCanonicalLocalEngine(value) {
    const normalized = String(value || '').trim().toLowerCase();
    return normalized === 'llama'
        || normalized === 'media'
        || normalized === 'speech'
        || normalized === 'sidecar';
}
function inferCanonicalLocalEngine(capability, engineLike, runtimeDefaultEngine) {
    const normalizedEngineLike = String(engineLike || '').trim().toLowerCase();
    if (isCanonicalLocalEngine(normalizedEngineLike)) {
        return normalizedEngineLike;
    }
    const normalizedDefault = String(runtimeDefaultEngine || '').trim().toLowerCase();
    if (isCanonicalLocalEngine(normalizedDefault)) {
        return normalizedDefault;
    }
    if (capability === 'image.generate' || capability === 'video.generate') {
        return 'media';
    }
    if (capability === 'audio.synthesize'
        || capability === 'audio.transcribe'
        || capability === 'voice_workflow.voice_clone'
        || capability === 'voice_workflow.voice_design') {
        return 'speech';
    }
    return 'llama';
}
function firstAvailableBinding(localModels, _connectors) {
    if (localModels.length > 0) {
        const firstLocal = localModels[0];
        return {
            source: 'local',
            connectorId: '',
            model: firstLocal.model,
            modelId: firstLocal.modelId,
            localModelId: firstLocal.localModelId,
            provider: firstLocal.provider,
            engine: firstLocal.engine,
            adapter: firstLocal.adapter,
            providerHints: firstLocal.providerHints,
            endpoint: firstLocal.endpoint,
            goRuntimeLocalModelId: firstLocal.goRuntimeLocalModelId,
            goRuntimeStatus: firstLocal.goRuntimeStatus,
        };
    }
    return null;
}
function toLocalBinding(option) {
    const modelId = String(option.modelId || option.model || '').trim();
    return {
        source: 'local',
        connectorId: '',
        model: modelId,
        modelId: modelId || undefined,
        provider: String(option.provider || option.engine || '').trim() || undefined,
        localModelId: String(option.localModelId || '').trim() || undefined,
        engine: String(option.engine || '').trim() || undefined,
        adapter: option.adapter,
        providerHints: option.providerHints,
        endpoint: String(option.endpoint || '').trim() || undefined,
        goRuntimeLocalModelId: String(option.goRuntimeLocalModelId || '').trim() || undefined,
        goRuntimeStatus: String(option.goRuntimeStatus || '').trim() || undefined,
    };
}
function bindingKey(input) {
    if (!input) {
        return '';
    }
    return [
        String(input.source || '').trim(),
        String(input.connectorId || '').trim(),
        String(input.modelId || input.model || '').trim(),
        String(input.localModelId || '').trim(),
        String(input.engine || '').trim(),
    ].join('|');
}
function hydrateSelectedLocalBinding(binding, localModels) {
    const bindingLocalModelId = String(binding.localModelId || '').trim();
    if (bindingLocalModelId) {
        const byLocalModelId = localModels.find((item) => String(item.localModelId || '').trim() === bindingLocalModelId) || null;
        if (byLocalModelId) {
            return toLocalBinding(byLocalModelId);
        }
    }
    const targetModelId = String(binding.modelId || binding.model || '').trim();
    const byModelId = localModels.find((item) => String(item.modelId || item.model || '').trim() === targetModelId) || null;
    if (byModelId) {
        return toLocalBinding(byModelId);
    }
    return {
        ...binding,
        model: String(binding.model || binding.modelId || '').trim(),
        modelId: String(binding.modelId || binding.model || '').trim() || undefined,
    };
}
function hydrateSelectedCloudBinding(binding, connectors) {
    const exactMatch = connectors
        .flatMap((connector) => connector.models.map((model) => ({
        source: 'cloud',
        connectorId: connector.id,
        model,
        provider: String(connector.provider || '').trim() || undefined,
    })))
        .find((item) => bindingKey(item) === bindingKey(binding)) || null;
    if (exactMatch) {
        return exactMatch;
    }
    const connector = connectors.find((item) => item.id === binding.connectorId) || null;
    if (!connector) {
        return {
            ...binding,
            connectorId: String(binding.connectorId || '').trim(),
            model: String(binding.model || binding.modelId || '').trim(),
        };
    }
    return {
        ...binding,
        connectorId: String(binding.connectorId || '').trim(),
        model: String(binding.model || binding.modelId || '').trim(),
        provider: String(binding.provider || connector.provider || '').trim() || undefined,
    };
}
export function normalizeRuntimeRouteCapabilityToken(value) {
    const normalized = String(value || '').trim().toLowerCase();
    if (normalized === 'text.generate'
        || normalized === 'text.embed'
        || normalized === 'image.generate'
        || normalized === 'video.generate'
        || normalized === 'world.generate'
        || normalized === 'audio.synthesize'
        || normalized === 'audio.transcribe'
        || normalized === 'music.generate'
        || normalized === 'voice_workflow.voice_clone'
        || normalized === 'voice_workflow.voice_design') {
        return normalized;
    }
    return normalizeCapabilityAlias(normalized);
}
export function runtimeRouteModelSupportsCapability(capabilities, capability) {
    return (capabilities || []).some((item) => normalizeRuntimeRouteCapabilityToken(item) === capability);
}
export function runtimeRouteLocalKindSupportsCapability(kind, capability) {
    const normalizedKind = String(kind || '').trim().toLowerCase();
    if (!normalizedKind) {
        return false;
    }
    if (capability === 'text.generate' && normalizedKind === 'chat') {
        return true;
    }
    if (capability === 'text.embed' && normalizedKind === 'embedding') {
        return true;
    }
    if (capability === 'image.generate' && normalizedKind === 'image') {
        return true;
    }
    if (capability === 'video.generate' && normalizedKind === 'video') {
        return true;
    }
    if (capability === 'audio.synthesize' && normalizedKind === 'tts') {
        return true;
    }
    if (capability === 'audio.transcribe' && normalizedKind === 'stt') {
        return true;
    }
    return false;
}
export function buildRuntimeRouteSelectedBinding(input) {
    const { selectedBinding, localModels, connectors, localMetadataDegraded, runtimeDefaultEngine, } = input;
    if (selectedBinding?.source === 'local') {
        const matchedLocalModel = hydrateSelectedLocalBinding(selectedBinding, localModels);
        if (String(matchedLocalModel.localModelId || '').trim() || localModels.some((item) => bindingKey(toLocalBinding(item)) === bindingKey(matchedLocalModel))) {
            const exactLocal = localModels.find((item) => bindingKey(toLocalBinding(item)) === bindingKey(matchedLocalModel)) || null;
            if (exactLocal) {
                return toLocalBinding(exactLocal);
            }
        }
        const engine = inferCanonicalLocalEngine(input.capability, matchedLocalModel.engine || matchedLocalModel.provider, runtimeDefaultEngine);
        return {
            ...matchedLocalModel,
            provider: isCanonicalLocalEngine(matchedLocalModel.provider)
                ? String(matchedLocalModel.provider || '').trim()
                : engine,
            engine,
            goRuntimeStatus: String(matchedLocalModel.goRuntimeStatus || '').trim()
                || (localMetadataDegraded ? 'degraded' : 'unavailable'),
        };
    }
    if (selectedBinding?.source === 'cloud') {
        return hydrateSelectedCloudBinding(selectedBinding, connectors);
    }
    return null;
}
export function buildRuntimeRouteOptionsSnapshot(input) {
    const selected = input.selectedOverride === undefined
        ? buildRuntimeRouteSelectedBinding({
            capability: input.capability,
            selectedBinding: input.selectedBinding,
            localModels: input.localModels,
            connectors: input.connectors,
            localMetadataDegraded: input.localMetadataDegraded,
            runtimeDefaultEngine: input.runtimeDefaultEngine,
        })
        : input.selectedOverride;
    const fallback = firstAvailableBinding(input.localModels, input.connectors);
    const resolvedDefault = (input.localMetadataDegraded && selected?.source === 'local')
        ? selected
        : (fallback || selected || undefined);
    return {
        capability: input.capability,
        selected,
        resolvedDefault,
        local: {
            models: input.localModels,
            defaultEndpoint: String(input.defaultLocalEndpoint || '').trim() || undefined,
        },
        connectors: input.connectors,
    };
}
//# sourceMappingURL=runtime-route-options.js.map