function toSearchText(value) {
    return String(value ?? '');
}
export function scoreRelevance(text, query) {
    if (!query)
        return 0;
    const lower = toSearchText(text).toLowerCase();
    const q = toSearchText(query).toLowerCase();
    if (lower === q)
        return 100;
    if (lower.startsWith(q))
        return 80;
    const wordBoundary = new RegExp(`\\b${q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`);
    if (wordBoundary.test(lower))
        return 60;
    if (lower.includes(q))
        return 40;
    return 0;
}
export function sortByRelevance(items, query, getSearchFields) {
    if (!query.trim())
        return items;
    return [...items].sort((a, b) => {
        const scoreA = Math.max(...getSearchFields(a).map((f) => scoreRelevance(f, query)));
        const scoreB = Math.max(...getSearchFields(b).map((f) => scoreRelevance(f, query)));
        return scoreB - scoreA;
    });
}
export function sortByColumn(items, sortConfig, getValue) {
    if (!sortConfig)
        return items;
    return [...items].sort((a, b) => {
        const aVal = getValue(a, sortConfig.key);
        const bVal = getValue(b, sortConfig.key);
        const cmp = typeof aVal === 'number' && typeof bVal === 'number'
            ? aVal - bVal
            : String(aVal).localeCompare(String(bVal));
        return sortConfig.direction === 'asc' ? cmp : -cmp;
    });
}
export function toggleSort(current, key) {
    if (current?.key === key) {
        return current.direction === 'asc' ? { key, direction: 'desc' } : { key, direction: 'asc' };
    }
    return { key, direction: 'asc' };
}
//# sourceMappingURL=search.js.map