var defaultSanitize = function defaultSanitize(str) {
    return str;
};
var defaultFindChunks = function defaultFindChunks(_a) {
    var autoEscape = _a.autoEscape, caseSensitive = _a.caseSensitive, _b = _a.sanitize, sanitize = _b === void 0 ? defaultSanitize : _b, searchWords = _a.searchWords, textToHighlight = _a.textToHighlight;
    textToHighlight = sanitize(textToHighlight);
    return searchWords
        .filter(function (searchWord) { return searchWord; })
        .reduce(function (chunks, searchWord) {
        if (typeof searchWord === 'string') {
            searchWord = sanitize(searchWord);
            if (autoEscape) {
                searchWord = escapeRegExpFn(searchWord);
            }
        }
        var regex = new RegExp(searchWord, caseSensitive ? 'g' : 'gi');
        var match;
        while ((match = regex.exec(textToHighlight))) {
            var start = match.index;
            var end = regex.lastIndex;
            if (end > start) {
                chunks.push({ highlight: false, start: start, end: end });
            }
            if (match.index === regex.lastIndex) {
                regex.lastIndex++;
            }
        }
        return chunks;
    }, []);
};
export function findAll(_a) {
    var autoEscape = _a.autoEscape, _b = _a.caseSensitive, caseSensitive = _b === void 0 ? false : _b, _c = _a.findChunks, findChunks = _c === void 0 ? defaultFindChunks : _c, sanitize = _a.sanitize, searchWords = _a.searchWords, textToHighlight = _a.textToHighlight;
    return fillInChunks({
        chunksToHighlight: combineChunks({
            chunks: findChunks({
                autoEscape: autoEscape,
                caseSensitive: caseSensitive,
                sanitize: sanitize,
                searchWords: searchWords,
                textToHighlight: textToHighlight,
            }),
        }),
        totalLength: textToHighlight ? textToHighlight.length : 0,
    });
}
function combineChunks(_a) {
    var chunks = _a.chunks;
    chunks = chunks
        .sort(function (first, second) { return first.start - second.start; })
        .reduce(function (processedChunks, nextChunk) {
        if (processedChunks.length === 0) {
            return [nextChunk];
        }
        else {
            var prevChunk = processedChunks.pop();
            if (nextChunk.start <= prevChunk.end) {
                var endIndex = Math.max(prevChunk.end, nextChunk.end);
                processedChunks.push({
                    highlight: false,
                    start: prevChunk.start,
                    end: endIndex,
                });
            }
            else {
                processedChunks.push(prevChunk, nextChunk);
            }
            return processedChunks;
        }
    }, []);
    return chunks;
}
function fillInChunks(_a) {
    var chunksToHighlight = _a.chunksToHighlight, totalLength = _a.totalLength;
    var allChunks = [];
    var append = function (start, end, highlight) {
        if (end - start > 0) {
            allChunks.push({
                start: start,
                end: end,
                highlight: highlight,
            });
        }
    };
    if (chunksToHighlight.length === 0) {
        append(0, totalLength, false);
    }
    else {
        var lastIndex_1 = 0;
        chunksToHighlight.forEach(function (chunk) {
            append(lastIndex_1, chunk.start, false);
            append(chunk.start, chunk.end, true);
            lastIndex_1 = chunk.end;
        });
        append(lastIndex_1, totalLength, false);
    }
    return allChunks;
}
function escapeRegExpFn(str) {
    return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
}
