import escape_html from "escape-html";
import { regexEncode } from "../../../../utils/string-utils.js";
function shouldUpdateFacetSearchComponent(nextFacetSearchState, prevFacetSearchState) {
    const hasQuery = '' !== nextFacetSearchState.query;
    const stillNoValues = !nextFacetSearchState.values.length && !prevFacetSearchState.values.length;
    const hasFinishedLoading = !nextFacetSearchState.isLoading && prevFacetSearchState.isLoading;
    if (hasQuery && stillNoValues) return hasFinishedLoading;
    return true;
}
function shouldDisplaySearchResults(facetSearchState) {
    const hasQuery = '' !== facetSearchState.query;
    const isLoading = facetSearchState.isLoading;
    const hasValues = !!facetSearchState.values.length;
    if (!hasQuery) return false;
    if (hasValues) return true;
    return !isLoading;
}
function highlightSearchResult(resultValue, searchQuery = '') {
    const sanitizedResult = escape_html(resultValue);
    if ('' === searchQuery.trim()) return sanitizedResult;
    const regex = new RegExp(`(${regexEncode(escape_html(searchQuery))})`, 'i');
    return escape_html(resultValue).replace(regex, '<span part="search-highlight" class="font-bold">$1</span>');
}
export { highlightSearchResult, shouldDisplaySearchResults, shouldUpdateFacetSearchComponent };
