export function fuzzyMatch(query, text) {
    const q = query.toLowerCase();
    const t = text.toLowerCase();
    if (q.length === 0)
        return { matches: true, score: 0 };
    if (q.length > t.length)
        return { matches: false, score: 0 };
    let qi = 0;
    let score = 0;
    let lastMatch = -1;
    let consecutive = 0;
    for (let i = 0; i < t.length && qi < q.length; i++) {
        if (t[i] === q[qi]) {
            const isBoundary = i === 0 || /[\s\-_./]/.test(t[i - 1]);
            if (lastMatch === i - 1) {
                consecutive++;
                score -= consecutive * 5;
            }
            else {
                consecutive = 0;
                if (lastMatch >= 0) {
                    score += (i - lastMatch - 1) * 2;
                }
            }
            if (isBoundary)
                score -= 10;
            score += i * 0.1;
            lastMatch = i;
            qi++;
        }
    }
    if (qi < q.length)
        return { matches: false, score: 0 };
    return { matches: true, score };
}
export function fuzzyFilter(items, query, getText) {
    if (!query.trim())
        return items;
    const scored = [];
    for (const item of items) {
        const match = fuzzyMatch(query, getText(item));
        if (match.matches)
            scored.push({ item, score: match.score });
    }
    scored.sort((a, b) => a.score - b.score);
    return scored.map((s) => s.item);
}
//# sourceMappingURL=fuzzy.js.map