function selectScore(result, hybridResult) {
    if (hybridResult) {
        return {
            score: hybridResult.similarity,
            source: 'hybrid',
            hybridSimilarity: hybridResult.similarity
        };
    }
    return {
        score: result.similarity,
        source: 'vector'
    };
}
function computeNormalizationRange(scores) {
    if (scores.length === 0) {
        return { min: 0, max: 1, denom: 1 };
    }
    const min = Math.min(...scores);
    const max = Math.max(...scores);
    const denom = max - min === 0 ? 1 : max - min;
    return { min, max, denom };
}
export class VectorSearchResultNormalizer {
    normalize(providerResults) {
        const unifiedMap = new Map();
        for (const providerResult of providerResults) {
            const vectorResults = providerResult.vectorResults ?? [];
            const hybridResults = providerResult.hybridResults ?? [];
            const hybridById = new Map();
            for (const hybrid of hybridResults) {
                hybridById.set(hybrid.memory_id, hybrid);
            }
            const scores = vectorResults.map(result => {
                const hybrid = hybridById.get(result.memory_id);
                return selectScore(result, hybrid).score;
            });
            const { min, denom } = computeNormalizationRange(scores);
            for (const vectorResult of vectorResults) {
                const hybrid = hybridById.get(vectorResult.memory_id);
                const scoreInfo = selectScore(vectorResult, hybrid);
                const normalizedScore = (scoreInfo.score - min) / denom;
                const key = `${vectorResult.memory_id}:${providerResult.provider}`;
                const existing = unifiedMap.get(key);
                if (!existing || normalizedScore > existing.normalizedScore) {
                    unifiedMap.set(key, {
                        memoryId: vectorResult.memory_id,
                        provider: providerResult.provider,
                        normalizedScore,
                        vectorSimilarity: vectorResult.similarity,
                        hybridSimilarity: scoreInfo.hybridSimilarity,
                        content: vectorResult.content,
                        type: vectorResult.type,
                        importance: vectorResult.importance,
                        createdAt: vectorResult.created_at,
                        tags: vectorResult.tags,
                        source: scoreInfo.source
                    });
                }
            }
        }
        return Array.from(unifiedMap.values()).sort((a, b) => b.normalizedScore - a.normalizedScore);
    }
}
export const vectorSearchResultNormalizer = new VectorSearchResultNormalizer();
//# sourceMappingURL=vector-search-result-normalizer.js.map