import { loadCatalogItems } from './repository.js';
export function computeSearchScore(item, query) {
    let score = 0;
    const id = item.id.toLowerCase();
    const name = item.name.toLowerCase();
    const capabilities = item.capabilities.map((capability) => capability.toLowerCase());
    if (id === query) {
        score += 120;
    }
    else if (id.includes(query)) {
        // else if: prevent exact matches from also scoring as partial matches
        score += 60;
    }
    if (name.includes(query)) {
        score += 50;
    }
    if (capabilities.some((capability) => capability.includes(query))) {
        score += 30;
    }
    return score;
}
export async function searchCatalog(query, opts = {}) {
    const items = await loadCatalogItems();
    const needle = query.toLowerCase();
    let results = items
        .map((item) => ({ item, score: computeSearchScore(item, needle) }))
        .filter((entry) => entry.score > 0)
        .sort((a, b) => b.score - a.score || a.item.id.localeCompare(b.item.id))
        .map((entry) => entry.item);
    if (opts.kind) {
        results = results.filter((item) => item.kind === opts.kind);
    }
    if (opts.provider) {
        results = results.filter((item) => item.provider === opts.provider);
    }
    if (opts.limit !== undefined) {
        results = results.slice(0, opts.limit);
    }
    return results;
}
