"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fuzzySearch = fuzzySearch;
function fuzzySearch(query, items) {
    if (!query) {
        return items;
    }
    const lowerCaseQuery = query.toLowerCase();
    const rankedItems = items
        .map((item, index) => {
        const lowerCaseItem = item.toLowerCase();
        let score = 0;
        let consecutiveMatches = 0;
        let lastMatchIndex = -1;
        for (let i = 0; i < lowerCaseQuery.length; i++) {
            const char = lowerCaseQuery[i];
            const foundIndex = lowerCaseItem.indexOf(char, lastMatchIndex + 1);
            if (foundIndex === -1) {
                return null;
            }
            if (i === 0) {
                score += Math.max(10, 50 - foundIndex);
            }
            if (foundIndex === 0 ||
                ['/', '\\', '_', '-', '.'].includes(lowerCaseItem[foundIndex - 1]) ||
                (/[a-z]/.test(item[foundIndex - 1]) && /[A-Z]/.test(item[foundIndex]))) {
                score += 15;
            }
            if (item[foundIndex] === query[i]) {
                score += 5;
            }
            if (lastMatchIndex === foundIndex - 1) {
                consecutiveMatches++;
                score += 20 * consecutiveMatches;
            }
            else {
                consecutiveMatches = 0;
            }
            score -= foundIndex - (lastMatchIndex + 1);
            lastMatchIndex = foundIndex;
        }
        score -= item.length * 0.1;
        return { item, score, index };
    })
        .filter((result) => result !== null);
    rankedItems.sort((a, b) => {
        if (a.score !== b.score) {
            return b.score - a.score;
        }
        return a.index - b.index;
    });
    return rankedItems.map((result) => result.item);
}
//# sourceMappingURL=fuzzy-search.js.map