import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import i18next from "i18next";
import _, { isEqual } from "lodash";
import React from "react";
import ReactDOM from "react-dom";
import { I18nextProvider, useTranslation } from "react-i18next";
import { useQuery } from "react-query";
import { ActiveFilter } from "../components/ActiveFilters";
import { CalendarMobile } from "../components/CalendarModal";
import { ChangeLanguage } from "../components/ChangeLanguage";
import { DataRangePicker, resetFilterCalendar, } from "../components/DateRangePicker";
import { DataRangePickerVertical } from "../components/DateRangePickerVertical";
import { DetailMemo } from "../components/Detail";
import { DetailMobileMemo } from "../components/DetailMobile";
import { FiltersMemo, SkeletonFilters } from "../components/Filters";
import { FiltersHorizontalMemo } from "../components/FiltersHorizontal";
import { FiltersMobileMemo } from "../components/FiltersMobile";
import { FiltersMobileLiveChangeMemo } from "../components/FiltersMobileLiveChange";
import GenerateResponse from "../components/GenerateResponse";
import ListPaginations from "../components/ListPaginations";
import { LoginInfoComponentMemo } from "../components/LoginInfo";
import { RemoveFilters } from "../components/RemoveFilters";
import { ResultsMemo } from "../components/ResultList";
import { ResultsPaginationMemo, SkeletonResult, } from "../components/ResultListPagination";
import { Search } from "../components/Search";
import { SearchMobile } from "../components/SearchMobile";
import { SearchWithSuggestions } from "../components/SearchWithSuggestions";
import SelectComponent from "../components/Select";
import { SimpleErrorBoundary } from "../components/SimpleErrorBoundary";
import CustomSkeleton from "../components/Skeleton";
import { SortResultListMemo } from "../components/SortResultList";
import { SortResultListCustomMemo } from "../components/SortResultListCustom";
import SortResults from "../components/SortResults";
import { TabsMemo, useTabTokens } from "../components/Tabs";
import { TotalResults } from "../components/TotalResults";
import { TotalResultsMobile } from "../components/TotalResultsMobile";
import { useOpenK9Client, } from "../components/client";
import { useDebounce } from "../components/useDebounce";
import { useRange } from "../components/useRange";
import { getAutoSelections, isOverlapping, useSelections, } from "../components/useSelections";
import "../i18n";
import AllFilters from "../components/AllFiltersConfigurable";
import Correction from "../components/Correction";
export function Main({ configuration, onConfigurationChange, onQueryStateChange, }) {
    const activeLanguage = i18next.language;
    //retrieving information from the configuration.
    const debounceTimeSearch = configuration.debounceTimeSearch || 600;
    const styledSkeletonResults = configuration.styledSkeletonResults || null;
    const extraClass = configuration.extraClass || null;
    const memoryResults = configuration.memoryResults || false;
    const queryStringMap = configuration.queryStringMap || null;
    const numberOfResults = configuration.numberResult || 10;
    const numberResultOfFilters = configuration.numberResultOfFilters || 10;
    const useGenerativeApi = configuration.useGenerativeApi;
    const useQueryString = configuration.useQueryString;
    const useQueryStringFilters = configuration.useQueryStringFilters;
    const useKeycloak = configuration.useKeycloak;
    const iconCustom = configuration.icons;
    const isHoverChangeDetail = configuration.resultList?.changeOnOver ?? true;
    const isActiveSkeleton = configuration?.isActiveSkeleton || null;
    const viewButton = configuration?.viewButton;
    const queryStringValues = configuration?.queryStringValues || [
        "text",
        "selection",
        "textOnChange",
        "filters",
    ];
    const skeletonCustom = {
        tabs: configuration.skeletonTabsCustom,
        results: configuration.skeletonResultsCustom,
        filters: configuration.skeletonFiltersCustom,
        suggestion: configuration.skeletonSuggestionCustom,
    };
    //state
    const { numberOfResults: numberOfResultsSearch, overrideSearchWithCorrection, setOverrideSearchWithCorrection, } = useRange();
    const [dynamicData, setDynamicData] = React.useState([]);
    const [isMobile, setIsMobile] = React.useState(false);
    const [isMobileMinWidth, setIsMobileMinWIdth] = React.useState(false);
    const [languageSelect, setLanguageSelect] = React.useState("");
    const [selectionsState, selectionsDispatch] = useSelections({
        useKeycloak,
        persistInQueryString: useQueryString,
        defaultString: configuration.defaultString || "",
        queryStringValues,
        queryStringMap,
    });
    const [selectionsStateSuggestions, selectionsDispatchSuggestions] = useSelections({
        useKeycloak,
        persistInQueryString: useQueryString,
        defaultString: configuration.defaultString || "",
        queryStringValues,
        queryStringMap,
    });
    const [sortAfterKey, setSortAfterKey] = React.useState("");
    const [totalResult, setTotalResult] = React.useState(null);
    const [prevSearchQuery, setPrevSearchQuery] = React.useState([]);
    const [prevSearchQueryMobile, setPrevSearchQueryMobile] = React.useState([]);
    const [viewButtonDetail, setViewButtonDetail] = React.useState(false);
    const { dateRange, setDateRange, dateTokens } = useDateTokens();
    const { filterTokens, addFilterToken, setFilterTokens, removeFilterToken, resetFilter, } = useFilters({
        configuration,
        onConfigurationChange,
        selectionsState,
        selectionsDispatch,
        useQueryStringFilters,
    });
    const { i18n } = useTranslation();
    const { tabs, selectedTabIndex, setSelectedTabIndex, tabTokens, sortList, setSort, isLoadingTab, } = useTabs(configuration.overrideTabs, languageSelect, configuration.autoselectTab ?? true);
    const { resetSort, setSelectedSort } = useSortResult({
        configuration,
        onConfigurationChange,
        setSortAfterKey,
        sortList,
        setSort,
    });
    const { dynamicFilters, languageQuery, whoIsDynamicResponse, languages, retrieveType, } = recoveryDataBackEnd();
    const { searchQuery, spans, isQueryAnalysisComplete, completelySort } = useSearch({
        configuration,
        debounceTimeSearch,
        tabTokens: { tabToken: tabTokens.tabTokens, sort: tabTokens.sort },
        filterTokens,
        dateTokens,
        onQueryStateChange,
        selectionsState,
        selectionsDispatch,
        retrieveType,
        externalSearchTokens: configuration.externalSearchTokens,
        setSortAfterKey,
    });
    const { isQueryAnalysisCompleteSuggestions, spansSuggestions } = useQueryAnalysisWithoutSearch({
        configuration,
        selectionsState: selectionsStateSuggestions,
        debounceTimeSearch,
    });
    const { detail, setDetail } = useDetails(searchQuery, setPrevSearchQuery, prevSearchQuery);
    const { detailMobile, setDetailMobile, idPreview, setIdPreview } = useDetailsMobile(searchQuery, prevSearchQueryMobile, setPrevSearchQueryMobile);
    //Effect
    React.useEffect(() => {
        const checkIfMobile = () => {
            const isMobileDevice = window.innerWidth <= 1024 && window.innerWidth >= 320;
            if (!isMobileDevice)
                setDetailMobile(null);
            setIsMobile(isMobileDevice);
        };
        checkIfMobile();
        window.addEventListener("resize", checkIfMobile);
        return () => {
            window.removeEventListener("resize", checkIfMobile);
        };
    }, []);
    React.useEffect(() => {
        const checkIfMobile = () => {
            const isMobileMinWidth = window.innerWidth <= 380;
            setIsMobileMinWIdth(isMobileMinWidth);
        };
        checkIfMobile();
        window.addEventListener("resize", checkIfMobile);
        return () => {
            window.removeEventListener("resize", checkIfMobile);
        };
    }, []);
    React.useEffect(() => {
        if (whoIsDynamicResponse.isSuccess) {
            const newData = factoryWhoIsDynamic({
                whoIsDynamicResponse: whoIsDynamicResponse.data,
            });
            setDynamicData(newData);
        }
    }, [whoIsDynamicResponse.isSuccess, whoIsDynamicResponse.data]);
    React.useEffect(() => {
        if (configuration.languageSelect) {
            i18n.changeLanguage(remappingLanguage({ language: configuration.languageSelect }));
            setLanguageSelect(configuration.languageSelect);
        }
        else if (languageQuery.data?.value) {
            i18n.changeLanguage(remappingLanguage({ language: languageQuery.data.value }));
            setLanguageSelect(languageQuery.data.value);
        }
    }, [languageQuery.data, i18n, configuration.languageSelect]);
    const isSearchLoading = dynamicFilters.isLoading ||
        languageQuery.isLoading ||
        whoIsDynamicResponse.isLoading ||
        languages.isLoading ||
        isLoadingTab;
    return (_jsxs(React.Fragment, { children: [renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(Search, { configuration: configuration, spans: spans, selectionsState: selectionsState, selectionsDispatch: selectionsDispatch, showSyntax: isQueryAnalysisComplete, viewColor: configuration.showSyntax, btnSearch: false, extraClass: extraClass?.classSearch }) }), configuration.search), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(Search, { configuration: configuration, spans: spans, selectionsState: selectionsState, selectionsDispatch: selectionsDispatch, showSyntax: isQueryAnalysisComplete, btnSearch: true, viewColor: configuration.showSyntax, extraClass: extraClass?.classSearch }) }), configuration.searchWithButton), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(Search, { configuration: configuration, extraClass: extraClass?.classSearch, spans: spans, selectionsState: selectionsState, selectionsDispatch: selectionsDispatch, showSyntax: isQueryAnalysisComplete, btnSearch: configuration.searchConfigurable?.btnSearch ?? true, htmlKey: configuration.searchConfigurable?.htmlKey, viewColor: configuration.showSyntax, messageSearchIsVisible: configuration?.searchConfigurable?.messageSearchIsVisible ?? true, customMessageSearch: configuration?.searchConfigurable?.customMessageSearch, actionOnClick: () => {
                        const functionCallback = configuration?.searchConfigurable?.actionOnClick;
                        if (functionCallback)
                            functionCallback();
                    }, callbackClickSearch: configuration?.searchConfigurable?.callbackClickSearch, characterControl: configuration?.searchConfigurable?.characterControl, callbackChangeSearch: configuration?.searchConfigurable?.callbackChangeSearch }) }), configuration.searchConfigurable
                ? configuration.searchConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SearchWithSuggestions, { configuration: configuration, spans: spansSuggestions, selectionsState: selectionsStateSuggestions, selectionsDispatch: selectionsDispatchSuggestions, showSyntax: isQueryAnalysisCompleteSuggestions, callbackSearch: configuration?.searchWithSuggestions?.callbackSearchButton, placeholder: configuration?.searchWithSuggestions?.placeholder, labelIcon: configuration?.searchWithSuggestions?.ariaLabelIcon }) }), configuration.searchWithSuggestions
                ? configuration.searchWithSuggestions.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(Correction, { setSearch: (value) => {
                        selectionsDispatch({
                            text: value,
                            type: "set-text",
                            textOnchange: value,
                        });
                    }, information: configuration.correction?.information || (() => null), onCorrectionCallback: () => setOverrideSearchWithCorrection((cor) => ({
                        isAutocorrection: false,
                        renderingCorrection: !cor.renderingCorrection,
                    })) }) }), configuration.correction ? configuration.correction.element : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: tabs.length >= 0 ? (_jsx(TabsMemo, { tabs: tabs, selectedTabIndex: selectedTabIndex, onSelectedTabIndexChange: setSelectedTabIndex, language: languageSelect, resetFilter: resetFilter, resetSort: resetSort, selectionsDispatch: selectionsDispatch })) : (skeletonCustom.tabs && isActiveSkeleton && skeletonCustom.tabs) }), configuration.tabs), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(RemoveFilters, { reset: {
                        filters: () => {
                            onConfigurationChange({ filterTokens: [] });
                            selectionsDispatch({ type: "reset-filters" });
                        },
                    } }) }), configuration.removeFilters), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(RemoveFilters, { itemsRemove: configuration?.removeFiltersConfigurable?.itemsRemove, configurableLabel: configuration.removeFiltersConfigurable?.label, removeAriaLabel: configuration.removeFiltersConfigurable?.ariaLabel, reset: {
                        calendar: () => {
                            setDateRange({
                                startDate: undefined,
                                endDate: undefined,
                                keywordKey: undefined,
                            });
                            resetFilterCalendar && resetFilterCalendar();
                        },
                        filters: () => {
                            onConfigurationChange({ filterTokens: [] });
                            selectionsDispatch({ type: "reset-filters" });
                        },
                        search: () => {
                            selectionsDispatch({
                                type: "reset-search",
                            });
                        },
                        sort: () => {
                            resetSort();
                        },
                        language: () => {
                            setLanguageSelect(activeLanguage);
                        },
                    }, callback: configuration?.removeFiltersConfigurable?.callback }) }), configuration.removeFiltersConfigurable
                ? configuration.removeFiltersConfigurable.element
                : null), renderPortal(useGenerativeApi ? (_jsx(GenerateResponse, { question: selectionsState.text, searchQuery: searchQuery, language: languageSelect, sortAfterKey: sortAfterKey, sort: completelySort })) : null, configuration.generateResponse), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(TabsMemo, { tabs: tabs, selectedTabIndex: selectedTabIndex, onSelectedTabIndexChange: setSelectedTabIndex, language: languageSelect, resetFilter: resetFilter, onAction: () => {
                        const callback = configuration.tabsConfigurable?.onAction;
                        if (callback)
                            callback();
                        selectionsDispatch({
                            type: "set-range",
                            range: [0, selectionsState.range[1]],
                        });
                    }, scrollMode: configuration.tabsConfigurable?.scrollMode, speed: configuration.tabsConfigurable?.speed, distance: configuration.tabsConfigurable?.distance, step: configuration.tabsConfigurable?.step, reset: configuration.tabsConfigurable?.reset, resetSort: resetSort, selectionsDispatch: selectionsDispatch, readMessageScreenReader: configuration.tabsConfigurable?.readMessageScreenReader, textLabelScreenReader: configuration.tabsConfigurable?.textLabelScreenReader, tabsCallbackProps: configuration.tabsConfigurable?.tabs, isUnselectTab: configuration.tabsConfigurable?.isUnselectTab ?? false }) }), configuration.tabsConfigurable
                ? configuration.tabsConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? (skeletonCustom.filters ? (isActiveSkeleton && skeletonCustom.filters) : null) : (_jsx(FiltersMemo, { state: selectionsState, searchQuery: searchQuery, onAddFilterToken: addFilterToken, numberOfResults: numberOfResults, onRemoveFilterToken: removeFilterToken, sort: completelySort, sortAfterKey: sortAfterKey, language: languageSelect, isDynamicElement: dynamicData, isActiveSkeleton: isActiveSkeleton?.filters ?? false, skeletonCategoryCustom: skeletonCustom.suggestion, memoryResults: memoryResults, iconCustom: iconCustom, setOverrideSearchWithCorrection: setOverrideSearchWithCorrection, overrideSearchWithCorrection: overrideSearchWithCorrection })) }), configuration.filters), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? (skeletonCustom.results ? (isActiveSkeleton && skeletonCustom.filters) : (isActiveSkeleton && _jsx(SkeletonFilters, {}))) : (_jsx(FiltersMemo, { state: selectionsState, searchQuery: searchQuery, onAddFilterToken: addFilterToken, onRemoveFilterToken: removeFilterToken, sort: completelySort, sortAfterKey: sortAfterKey, language: languageSelect, numberItems: configuration.filtersConfigurable?.numberItems, numberOfResults: numberOfResults, isDynamicElement: dynamicData, noResultMessage: configuration.filtersConfigurable?.noResultMessage, isActiveSkeleton: isActiveSkeleton?.filters ?? false, skeletonCategoryCustom: skeletonCustom.suggestion, memoryResults: memoryResults, placeholder: configuration.filtersConfigurable?.placeholder, haveSearch: configuration.filtersConfigurable?.haveSearch, iconCustom: iconCustom, setOverrideSearchWithCorrection: setOverrideSearchWithCorrection, overrideSearchWithCorrection: overrideSearchWithCorrection })) }), configuration.filtersConfigurable
                ? configuration.filtersConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(ActiveFilter, { searchQuery: searchQuery, onRemoveFilterToken: removeFilterToken, onConfigurationChange: onConfigurationChange }) }), configuration.activeFilters), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(ActiveFilter, { searchQuery: searchQuery, onRemoveFilterToken: removeFilterToken, onConfigurationChange: onConfigurationChange, actioneRemoveFilters: configuration.activeFiltersConfigurable?.actioneRemoveFilters, callbackRemoveFilter: configuration.activeFiltersConfigurable?.callbackRemoveFilter }) }), configuration.activeFiltersConfigurable
                ? configuration.activeFiltersConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(FiltersHorizontalMemo, { searchQuery: searchQuery, isDynamicElement: dynamicData, numberOfResults: numberOfResults, numberResultOfFilters: numberResultOfFilters, sortAfterKey: sortAfterKey, onConfigurationChange: onConfigurationChange, selectionsDispatch: selectionsDispatch, onConfigurationChangeExt: configuration.filtersHorizontal
                        ? configuration.filtersHorizontal.callback
                        : () => { }, sort: completelySort, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, language: languageSelect, refButton: configuration.filtersHorizontal?.refButton, callbackSubmit: configuration.filtersHorizontal
                        ? configuration.filtersHorizontal.callbackSubmit
                        : () => { }, callbackReset: configuration.filtersHorizontal
                        ? configuration.filtersHorizontal.callbackReset
                        : () => { }, memoryResults: memoryResults }) }), configuration.filtersHorizontal
                ? configuration.filtersHorizontal.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? (skeletonCustom.results ? (isActiveSkeleton && skeletonCustom.results) : (isActiveSkeleton && (_jsx(SkeletonResult, { background: styledSkeletonResults?.background })))) : (_jsx(ResultsMemo, { setTotalResult: setTotalResult, displayMode: configuration.resultsDisplayMode, searchQuery: searchQuery, onDetail: setDetail, setDetailMobile: setDetailMobile, sort: completelySort, setSortResult: setSort, isMobile: isMobile, overChangeCard: true, language: languageSelect, setSortAfterKey: setSortAfterKey, sortAfterKey: sortAfterKey, numberOfResults: numberOfResults, setIdPreview: setIdPreview, selectOptions: sortList, memoryResults: memoryResults, viewButton: viewButton, templateCustom: configuration.template, setViewButtonDetail: setViewButtonDetail, setSelectedSort: setSelectedSort })) }), configuration.results), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(TotalResults, { totalResult: totalResult }) }), configuration.totalResult), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(TotalResults, { totalResult: totalResult, saveTotalResultState: configuration.totalResultConfigurable?.saveTotalResultState }) }), configuration.totalResultConfigurable
                ? configuration.totalResultConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(TotalResultsMobile, { totalResult: totalResult }) }), configuration.totalResultMobile), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? (skeletonCustom.results ? (isActiveSkeleton && skeletonCustom.results) : (isActiveSkeleton && (_jsx(SkeletonResult, { background: styledSkeletonResults?.background })))) : (_jsx(_Fragment, { children: _jsx(ResultsMemo, { setTotalResult: setTotalResult, displayMode: configuration.resultsDisplayMode, searchQuery: searchQuery, onDetail: setDetail, setDetailMobile: setDetailMobile, sort: completelySort, setSortResult: setSort, isMobile: isMobile, overChangeCard: configuration.resultList?.changeOnOver || false, language: languageSelect, setSortAfterKey: setSortAfterKey, sortAfterKey: sortAfterKey, numberOfResults: numberOfResults, setIdPreview: setIdPreview, setSelectedSort: setSelectedSort, counterIsVisible: configuration.resultList?.counterIsVisible || false, selectOptions: sortList, memoryResults: memoryResults, viewButton: viewButton, setViewButtonDetail: setViewButtonDetail, NoResultsCustom: configuration.resultList?.noResultsCustom, templateCustom: configuration.template }) })) }), configuration.resultList ? configuration.resultList.element : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? (skeletonCustom.results ? (isActiveSkeleton && skeletonCustom.results) : (isActiveSkeleton && (_jsx(SkeletonResult, { background: styledSkeletonResults?.background })))) : (_jsxs(_Fragment, { children: [_jsx(ResultsPaginationMemo, { setTotalResult: setTotalResult, displayMode: configuration.resultsDisplayMode, searchQuery: searchQuery, onDetail: setDetail, setDetailMobile: setDetailMobile, sort: completelySort, setSortResult: setSort, isMobile: isMobile, overChangeCard: false, language: languageSelect, sortAfterKey: sortAfterKey, callback: configuration.resultListPagination?.callback, state: selectionsState, dispatch: selectionsDispatch, CustomNoResults: configuration.resultListPagination?.noResultsCustom, backgroundSkeleton: styledSkeletonResults?.background }), numberOfResultsSearch > 0 && (_jsx(ListPaginations, { itemsPerPage: numberOfResults, state: selectionsState, dispatch: selectionsDispatch, extraClass: extraClass?.classResultsListPagination }))] })) }), configuration.resultListPagination
                ? configuration.resultListPagination.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(CustomSkeleton, { backgroundColor: configuration.skeleton?.backgroundColor, circle: configuration.skeleton?.circle, containerMax: configuration.skeleton?.containerMax, counter: configuration.skeleton?.counter, gap: configuration.skeleton?.gap, height: configuration.skeleton?.height, itereitorKey: configuration.skeleton?.itereitorKey, position: configuration.skeleton?.position, width: configuration.skeleton?.width }) }), configuration.sortable), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DetailMemo, { result: detail, actionOnCLose: () => { }, template: configuration.template, cardDetailsOnOver: isHoverChangeDetail, callbackFocusedButton: () => {
                        const recoveryButton = document.getElementById("openk9-button-card-" + idPreview);
                        if (recoveryButton)
                            recoveryButton.focus();
                        setDetail(null);
                    }, setViewButtonDetail: setViewButtonDetail, viewButtonDetail: viewButtonDetail }) }), configuration.details), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(LoginInfoComponentMemo, { isMobile: isMobileMinWidth }) }), configuration.login), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SortResultListCustomMemo, { classTab: tabs[selectedTabIndex]?.label
                        .replaceAll(" ", "-")
                        .toLowerCase(), setSortResult: setSort, selectOptions: configuration.sortResultListCustom?.selectOptions ?? [], labelSelect: configuration.sortResultListCustom?.labelSort || "ordina per" }) }), configuration.sortResultListCustom
                ? configuration.sortResultListCustom.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SortResultListMemo, { setSortResult: setSort }) }), configuration.sortable), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SortResultListMemo, { setSortResult: setSort, relevance: configuration.sortableConfigurable?.relevance }) }), configuration.sortableConfigurable
                ? configuration.sortableConfigurable.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SortResultListMemo, { setSortResult: setSort, relevance: configuration.sortResultConfigurable?.relevance, HtmlString: configuration.sortResultConfigurable?.htmlKey || undefined }) }), configuration.sortResultConfigurable
                ? configuration.sortResultConfigurable?.sort
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(ChangeLanguage, { setChangeLanguage: (val) => {
                        setLanguageSelect(val);
                        i18n.changeLanguage(remappingLanguage({ language: val }));
                    }, languages: languages.data, activeLanguage: languageSelect }) }), configuration.changeLanguage), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SelectComponent, { language: languageSelect, selectOptions: [], extraClass: configuration.select?.extraClass, labelDefault: "", handleChange: configuration.select?.handleChange }) }), configuration.select ? configuration.select?.element : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SortResults, { language: languageSelect, selectOptions: sortList ?? [], extraClass: configuration.sortResults?.extraClass, labelDefault: configuration.sortResults?.defaultLabelName, labelText: configuration.sortResults?.labelText, classNameLabel: configuration.sortResults?.classNameLabel, setSort: setSort, sort: tabTokens.sort }) }), configuration.sortResults ? configuration.sortResults?.element : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DetailMobileMemo, { result: detailMobile, setDetailMobile: setDetailMobile, cardDetailsOnOver: isHoverChangeDetail, onClose: () => {
                        const recoveryButton = document.getElementById("preview-card-" + idPreview);
                        if (recoveryButton)
                            recoveryButton.focus();
                    }, template: configuration.template }) }), configuration.detailMobile), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? null : (_jsx(FiltersMobileMemo, { searchQuery: searchQuery, onConfigurationChange: onConfigurationChange, filtersSelect: configuration.filterTokens, sort: completelySort, selectionsDispatch: selectionsDispatch, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, isVisibleFilters: true, setIsVisibleFilters: undefined, language: languageSelect, sortAfterKey: sortAfterKey, isDynamicElement: dynamicData, numberResultOfFilters: numberResultOfFilters, memoryResults: memoryResults, filtersMobileBasicCallback: configuration.filtersMobileBasicCallback })) }), configuration.filtersMobileBasic), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? null : (_jsx(FiltersMobileMemo, { searchQuery: searchQuery, onConfigurationChange: onConfigurationChange, filtersSelect: configuration.filterTokens, sort: completelySort, selectionsDispatch: selectionsDispatch, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, isVisibleFilters: configuration.filtersMobile?.isVisible || false, setIsVisibleFilters: configuration.filtersMobile?.setIsVisible, language: languageSelect, sortAfterKey: sortAfterKey, isDynamicElement: dynamicData, numberResultOfFilters: numberResultOfFilters, memoryResults: memoryResults })) }), configuration.filtersMobile?.element !== undefined
                ? configuration.filtersMobile?.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? null : (_jsx(FiltersMobileLiveChangeMemo, { searchQuery: searchQuery, onAddFilterToken: addFilterToken, onRemoveFilterToken: removeFilterToken, onConfigurationChange: onConfigurationChange, sortAfterKey: sortAfterKey, filtersSelect: configuration.filterTokens, sort: completelySort, skeletonCategoryCustom: skeletonCustom.suggestion, isActiveSkeleton: isActiveSkeleton?.filters ?? false, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, configuration: configuration, whoIsDynamic: dynamicData, numberItems: configuration.filtersConfigurable?.numberItems, isVisibleFilters: configuration.filtersMobileLiveChange?.isVisible || false, setIsVisibleFilters: configuration.filtersMobileLiveChange?.setIsVisible, tabs: tabs, onSelectedTabIndexChange: setSelectedTabIndex, selectedTabIndex: selectedTabIndex, viewTabs: configuration.filtersMobileLiveChange?.viewTabs ?? false, language: languageSelect, isCollapsable: configuration.filtersMobileLiveChange?.isCollapsable ?? true, numberOfResults: numberOfResults, selectionsDispatch: selectionsDispatch, memoryResults: memoryResults, haveSearch: configuration.filtersMobileLiveChange?.haveSearch, state: selectionsState })) }), configuration.filtersMobileLiveChange?.element !== undefined
                ? configuration.filtersMobileLiveChange?.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: isSearchLoading ? null : (_jsx(FiltersMobileLiveChangeMemo, { searchQuery: searchQuery, state: selectionsState, addExtraClass: configuration.mobileFiltersBasicLiveChange?.addExtraClass ||
                        undefined, onAddFilterToken: addFilterToken, onRemoveFilterToken: removeFilterToken, onConfigurationChange: onConfigurationChange, sortAfterKey: sortAfterKey, filtersSelect: configuration.filterTokens, sort: completelySort, skeletonCategoryCustom: skeletonCustom.suggestion, isActiveSkeleton: isActiveSkeleton?.filters ?? false, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, configuration: configuration, whoIsDynamic: dynamicData, isVisibleFilters: true, setIsVisibleFilters: undefined, tabs: tabs, onSelectedTabIndexChange: setSelectedTabIndex, selectedTabIndex: selectedTabIndex, viewTabs: configuration.mobileFiltersBasicLiveChange?.viewTabs ?? false, language: languageSelect, isCollapsable: true, numberOfResults: numberOfResults, selectionsDispatch: selectionsDispatch, memoryResults: memoryResults, haveSearch: true, callbackClose: configuration?.mobileFiltersBasicLiveChange
                        ?.closeFiltersMobileLiveChangeCallback, callbackApply: configuration?.mobileFiltersBasicLiveChange
                        ?.applyfiltersMobileLiveChangeCallback })) }), configuration.filtersMobileLiveChangeBasic), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DataRangePicker, { onChange: setDateRange, calendarDate: dateRange, start: configuration.dataRangePicker?.start, end: configuration.dataRangePicker?.end, language: languageSelect }) }), configuration.dataRangePicker?.element !== undefined
                ? configuration.dataRangePicker?.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DataRangePickerVertical, { onChange: setDateRange, calendarDate: dateRange, language: languageSelect, start: configuration.dataRangePickerVertical?.start, end: configuration.dataRangePickerVertical?.end, classTab: tabs[selectedTabIndex]?.label
                        .replaceAll(" ", "-")
                        .toLowerCase(), readOnly: configuration.dataRangePickerVertical?.readOnly ?? false, translationLabel: configuration.dataRangePickerVertical?.internationalLabel }) }), configuration.dataRangePickerVertical?.element !== undefined
                ? configuration.dataRangePickerVertical?.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(AllFilters, { filtersUse: configuration.allFilters?.typeFilters, defaultLanguage: languageSelect, filterDefault: {
                        state: selectionsState,
                        searchQuery: searchQuery,
                        onAddFilterToken: addFilterToken,
                        onRemoveFilterToken: removeFilterToken,
                        sort: completelySort,
                        sortAfterKey: sortAfterKey,
                        language: languageSelect,
                        numberItems: configuration.filtersConfigurable?.numberItems,
                        numberOfResults: numberOfResults,
                        isDynamicElement: dynamicData,
                        noResultMessage: configuration.filtersConfigurable?.noResultMessage,
                        isActiveSkeleton: isActiveSkeleton?.filters ?? false,
                        skeletonCategoryCustom: skeletonCustom.suggestion,
                        memoryResults: memoryResults,
                        placeholder: configuration.filtersConfigurable?.placeholder,
                        haveSearch: configuration.filtersConfigurable?.haveSearch,
                        iconCustom: iconCustom,
                        setAllFilters: setFilterTokens,
                        setLanguageSelected: setLanguageSelect,
                        setCalendarSelected: setDateRange,
                        setSortSelected: setSort,
                        languages: languages.data,
                        defaultFilter: selectionsState.filters,
                        setOverrideSearchWithCorrection: setOverrideSearchWithCorrection,
                        overrideSearchWithCorrection: overrideSearchWithCorrection,
                        numberResultOfFilters: numberResultOfFilters,
                    }, callback: {
                        save: configuration?.allFilters?.callbackSave ?? (() => { }),
                        reset: configuration?.allFilters?.callbackReset ?? (() => { }),
                    }, calendar: {
                        calendarDate: dateRange,
                        translationLabel: configuration.allFilters?.translationLabel,
                        onChange: setDateRange,
                    } }) }), configuration?.allFilters?.element
                ? configuration.allFilters.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DataRangePickerVertical, { onChange: setDateRange, calendarDate: dateRange, language: languageSelect, classTab: tabs[selectedTabIndex]?.label
                        .replaceAll(" ", "-")
                        .toLowerCase(), readOnly: configuration.dataRangePickerVertical?.readOnly ?? false, translationLabel: configuration.dataRangePickerVertical?.internationalLabel }) }), configuration.calendarVertical), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(DataRangePicker, { onChange: setDateRange, calendarDate: dateRange, language: languageSelect }) }), configuration.calendar), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(CalendarMobile, { onChange: setDateRange, calendarDate: dateRange, isVisibleCalendar: configuration.calendarMobile?.isVisible || false, setIsVisibleCalendar: configuration.calendarMobile?.setIsVisible, startDate: configuration.calendarMobile?.startDate, endDate: configuration.calendarMobile?.endDate, focusedInput: configuration.calendarMobile?.focusedInput, setStartDate: configuration.calendarMobile?.setStartDate, setEndDate: configuration.calendarMobile?.setEndDate, setFocusedInput: configuration.calendarMobile?.setFocusedInput, activeLanguage: activeLanguage, isCLickReset: configuration.calendarMobile?.isCLickReset || false, setIsCLickReset: configuration.calendarMobile?.setIsCLickReset, language: languageSelect }) }), configuration.calendarMobile?.element !== undefined
                ? configuration.calendarMobile?.element
                : null), renderPortal(_jsx(I18nextProvider, { i18n: i18next, children: _jsx(SearchMobile, { configuration: configuration, onConfigurationChange: onConfigurationChange, spans: spans, selectionsState: selectionsState, selectionsDispatch: selectionsDispatch, showSyntax: isQueryAnalysisComplete, onDetail: setDetail, dateRange: dateRange, onDateRangeChange: setDateRange, isMobile: isMobile, setSortResult: setSort, searchQuery: searchQuery, onAddFilterToken: addFilterToken, onRemoveFilterToken: removeFilterToken, filtersSelect: configuration.filterTokens, sort: completelySort, dynamicFilters: dynamicFilters.data?.handleDynamicFilters || false, isVisible: configuration.searchMobile?.isVisible || false, setIsVisible: configuration.searchMobile?.setIsVisible, btnSearch: configuration.searchConfigurable?.btnSearch ?? false }) }), configuration.searchMobile?.search !== undefined
                ? configuration.searchMobile?.search
                : null)] }));
}
function useStableValue(value) {
    const ref = React.useRef(value);
    if (!isEqual(ref.current, value)) {
        ref.current = value;
    }
    return ref.current;
}
function useFlushableDebounce(value, delay, flushKey) {
    const [debouncedValue, setDebouncedValue] = React.useState(value);
    const flushKeyRef = React.useRef(flushKey);
    const isFirstRef = React.useRef(true);
    React.useEffect(() => {
        if (isFirstRef.current) {
            isFirstRef.current = false;
            flushKeyRef.current = flushKey;
            return;
        }
        if (!Object.is(flushKeyRef.current, flushKey)) {
            flushKeyRef.current = flushKey;
            setDebouncedValue(value);
            return;
        }
        const timeout = setTimeout(() => setDebouncedValue(value), delay);
        return () => clearTimeout(timeout);
    }, [value, delay, flushKey]);
    return debouncedValue;
}
function useSearch({ configuration, debounceTimeSearch, tabTokens, filterTokens, dateTokens, onQueryStateChange, selectionsState, selectionsDispatch, retrieveType, externalSearchTokens, setSortAfterKey, }) {
    const { searchAutoselect, searchReplaceText, defaultTokens, sort } = configuration;
    const debouncedOnChange = useDebounce({
        textOnChange: selectionsState.textOnChange,
        selection: selectionsState.selection,
    }, debounceTimeSearch);
    const infoSort = tabTokens?.sort?.sort;
    const { numberOfResults } = useRange();
    const queryAnalysisSuggestions = !configuration.useQueryAnalysis
        ? { data: undefined, isPreviousData: false }
        : useQueryAnalysis({
            searchText: debouncedOnChange.textOnChange,
            tokens: debouncedOnChange.selection.flatMap(({ text, start, end, token }) => token ? [{ text, start, end, token }] : []),
        });
    const spansSuggestions = React.useMemo(() => calculateSpans(selectionsState.textOnChange, queryAnalysisSuggestions.data?.analysis), [selectionsState.textOnChange, queryAnalysisSuggestions.data?.analysis]);
    const queryAnalysisSearch = !configuration.useQueryAnalysis
        ? { data: undefined, isPreviousData: false }
        : useQueryAnalysis({
            searchText: selectionsState.text,
            tokens: selectionsState.selection.flatMap(({ text, start, end, token }) => token ? [{ text, start, end, token }] : []),
        });
    const spansSearch = React.useMemo(() => calculateSpans(selectionsState.text, queryAnalysisSearch.data?.analysis), [selectionsState.text, queryAnalysisSearch.data?.analysis]);
    const [previousSearchTokens, setPreviousSearchTokens] = React.useState([]);
    const computedSearchTokens = React.useMemo(() => {
        const selectionEntries = selectionsState.selection.flatMap(({ text, start, end, token }) => token ? [{ text, start, end, token }] : []);
        return deriveSearchQuery(spansSearch, selectionEntries, selectionsState.text, retrieveType || "TEXT").filter(Boolean);
    }, [
        spansSearch,
        selectionsState.selection,
        selectionsState.text,
        retrieveType,
    ]);
    const searchTokens = React.useMemo(() => {
        if (selectionsState.text !== selectionsState.textOnChange) {
            return previousSearchTokens;
        }
        setPreviousSearchTokens(computedSearchTokens);
        return computedSearchTokens;
    }, [
        computedSearchTokens,
        previousSearchTokens,
        selectionsState.text,
        selectionsState.textOnChange,
    ]);
    const newSearch = React.useMemo(() => searchTokens
        .filter((t) => (t.values ?? []).some((v) => (v ?? "").trim().length > 0))
        .map((t) => ({ ...t, search: true })), [searchTokens]);
    const newTokenFilter = React.useMemo(() => createFilter(filterTokens), [filterTokens]);
    const completelySort = React.useMemo(() => infoSort?.field
        ? [
            {
                [infoSort.field]: {
                    sort: infoSort.type,
                    missing: "_last",
                },
            },
        ]
        : sort || [], [infoSort?.field, infoSort?.type, sort]);
    const stableExternalSearchTokens = useStableValue(externalSearchTokens ?? null);
    const externalOverrides = React.useMemo(() => {
        const map = {};
        if (Array.isArray(stableExternalSearchTokens)) {
            for (const entry of stableExternalSearchTokens) {
                if (!entry || typeof entry !== "object" || !("kind" in entry))
                    continue;
                const tokens = Array.isArray(entry.tokens) ? entry.tokens : [];
                if (entry.kind === "tabs")
                    map.tabs = tokens;
                else if (entry.kind === "search")
                    map.search = tokens;
                else if (entry.kind === "filters")
                    map.filters = tokens;
            }
        }
        return map;
    }, [stableExternalSearchTokens]);
    const effectiveTabTokens = externalOverrides.tabs ?? tabTokens?.tabToken ?? [];
    const effectiveFilterTokens = externalOverrides.filters ?? newTokenFilter;
    const effectiveSearchTokens = externalOverrides.search ?? newSearch;
    const searchQueryMemo = React.useMemo(() => [
        ...defaultTokens,
        ...effectiveTabTokens,
        ...effectiveFilterTokens,
        ...effectiveSearchTokens,
        ...dateTokens,
    ], [
        defaultTokens,
        effectiveTabTokens,
        effectiveFilterTokens,
        effectiveSearchTokens,
        dateTokens,
    ]);
    const searchQuery = useFlushableDebounce(searchQueryMemo, debounceTimeSearch, stableExternalSearchTokens);
    const isFirstExternalRunRef = React.useRef(true);
    const pageSizeRef = React.useRef(selectionsState.range[1]);
    pageSizeRef.current = selectionsState.range[1];
    React.useEffect(() => {
        if (isFirstExternalRunRef.current) {
            isFirstExternalRunRef.current = false;
            return;
        }
        selectionsDispatch({
            type: "set-range",
            range: [0, pageSizeRef.current],
        });
        setSortAfterKey("");
    }, [stableExternalSearchTokens, selectionsDispatch, setSortAfterKey]);
    const isQueryAnalysisComplete = selectionsState.textOnChange === debouncedOnChange.textOnChange &&
        queryAnalysisSuggestions.data !== undefined &&
        !queryAnalysisSuggestions.isPreviousData;
    const counterTotalFilters = React.useMemo(() => newTokenFilter.reduce((total, element) => total + (element?.values?.length || 0), 0), [newTokenFilter]);
    React.useEffect(() => {
        onQueryStateChange({
            defaultTokens,
            tabTokens: tabTokens?.tabToken ?? [],
            filterTokens,
            searchTokens,
            numberOfFilters: counterTotalFilters,
            numberOfResults,
        });
    }, [
        onQueryStateChange,
        defaultTokens,
        tabTokens?.tabToken,
        filterTokens,
        searchTokens,
        counterTotalFilters,
        numberOfResults,
    ]);
    React.useEffect(() => {
        if (searchAutoselect &&
            queryAnalysisSuggestions.data &&
            queryAnalysisSuggestions.data.searchText === selectionsState.textOnChange) {
            const autoSelections = getAutoSelections(selectionsState.selection, queryAnalysisSuggestions.data.analysis);
            for (const selection of autoSelections) {
                selectionsDispatch({
                    type: "set-selection",
                    replaceText: false,
                    selection: {
                        token: selection.token,
                        end: selection.end,
                        isAuto: selection.isAuto,
                        start: selection.start,
                        text: selection.text,
                        textOnChange: "textOnChange" in selection &&
                            typeof selection.textOnChange === "string"
                            ? selection.textOnChange
                            : "",
                    },
                });
            }
        }
    }, [
        searchAutoselect,
        searchReplaceText,
        selectionsDispatch,
        queryAnalysisSuggestions.data,
        selectionsState.selection,
        selectionsState.textOnChange,
    ]);
    return {
        searchQuery,
        spans: spansSuggestions,
        isQueryAnalysisComplete,
        completelySort,
    };
}
function useQueryAnalysisWithoutSearch({ configuration, debounceTimeSearch, selectionsState, }) {
    const debounced = useDebounce(selectionsState, debounceTimeSearch);
    const queryAnalysis = !configuration.useQueryAnalysis
        ? { data: undefined }
        : useQueryAnalysis({
            searchText: debounced.textOnChange,
            tokens: debounced.selection.flatMap(({ text, start, end, token }) => token ? [{ text, start, end, token }] : []),
        });
    const spans = React.useMemo(() => calculateSpans(selectionsState.textOnChange, queryAnalysis.data?.analysis), [queryAnalysis.data?.analysis, selectionsState.textOnChange]);
    const isQueryAnalysisComplete = !configuration.useQueryAnalysis ||
        (selectionsState.textOnChange === debounced.textOnChange &&
            queryAnalysis.data !== undefined &&
            !queryAnalysis.isPreviousData);
    return {
        spansSuggestions: spans,
        isQueryAnalysisCompleteSuggestions: isQueryAnalysisComplete,
    };
}
function useTabs(overrideTabs, language, autoSelectFirstTab = true) {
    const [selectedTabIndex, setSelectedTabIndex] = React.useState(autoSelectFirstTab ? 0 : -1);
    const tenantTabs = useTabTokens();
    const tabs = React.useMemo(() => overrideTabs(tenantTabs.tab), [tenantTabs, overrideTabs, language]);
    const tabSelected = React.useMemo(() => tabs[selectedTabIndex], [tabs, selectedTabIndex]);
    const sortList = tabSelected?.sortings || undefined;
    const defaultTabTokens = React.useMemo(() => {
        const createTab = tabs[selectedTabIndex]?.tokens;
        const sort = sortList && sortList.find((sortElement) => sortElement.isDefault);
        const completeTab = createTab?.map((tab) => ({ ...tab, isTab: true }));
        return {
            tabTokens: completeTab,
            sort: sort?.field
                ? { sort: { field: sort.field, type: sort.type }, isSort: true }
                : undefined,
        };
    }, [selectedTabIndex, tabs, language]);
    const [sortOverride, setSortOverride] = React.useState(null);
    const prevTabIndexRef = React.useRef(selectedTabIndex);
    if (prevTabIndexRef.current !== selectedTabIndex) {
        prevTabIndexRef.current = selectedTabIndex;
        if (sortOverride !== null) {
            setSortOverride(null);
        }
    }
    const tabsValue = React.useMemo(() => ({
        tabTokens: defaultTabTokens.tabTokens,
        sort: sortOverride !== null ? sortOverride : defaultTabTokens.sort,
    }), [defaultTabTokens, sortOverride]);
    const setSort = (sortField) => {
        setSortOverride(sortField ? { sort: sortField, isSort: true } : undefined);
    };
    return {
        tabTokens: tabsValue,
        tabs,
        selectedTabIndex,
        setSelectedTabIndex,
        sortList,
        tabSelected,
        setSort,
        isLoadingTab: tenantTabs.isLoading,
    };
}
function recoveryDataBackEnd() {
    const client = useOpenK9Client();
    const dynamicFilters = useQuery(["handle-dynamic-filters", {}], async () => {
        return await client.handle_dynamic_filters();
    });
    const languageQuery = useQuery(["language", {}], async () => {
        return await client.getLanguageDefault();
    });
    const whoIsDynamicResponse = useQuery(["refresh-components", {}], async () => {
        return await client.getRefreshFilters();
    });
    const languages = useQuery(["date-label-languages", {}], async () => {
        return await client.getLanguages();
    });
    return {
        dynamicFilters,
        languageQuery,
        whoIsDynamicResponse,
        languages,
        retrieveType: whoIsDynamicResponse.data?.retrieveType,
    };
}
function useFilters({ configuration, onConfigurationChange, selectionsState, selectionsDispatch, useQueryStringFilters, }) {
    const filterTokens = configuration.useFilterConfiguration
        ? [...configuration.filterTokens, ...selectionsState.filters]
        : useQueryStringFilters
            ? selectionsState.filters
            : [];
    const setFilterTokens = React.useCallback((filters) => {
        onConfigurationChange((configuration) => ({
            ...configuration,
            selectionsState: { ...filters },
        }));
        selectionsDispatch({ type: "set-all-filters", filter: filters });
    }, [onConfigurationChange]);
    const addFilterToken = React.useCallback((searchToken) => {
        const newFilters = configuration.filterTokens.map((token) => {
            if (token.suggestionCategoryId === searchToken.suggestionCategoryId) {
                const updatedToken = {
                    ...token,
                    values: token.values
                        ? [...token.values, ...(searchToken.values ?? [])]
                        : [...(searchToken.values ?? [])],
                };
                return updatedToken;
            }
            return token;
        });
        selectionsDispatch({ type: "set-filters", filter: searchToken });
        onConfigurationChange((configuration) => ({
            ...configuration,
            filterTokens: [...newFilters],
        }));
    }, [configuration.filterTokens, onConfigurationChange, selectionsDispatch]);
    const removeFilterToken = React.useCallback((searchToken) => {
        selectionsDispatch({ type: "remove-filter", filter: searchToken });
        onConfigurationChange((configuration) => ({
            filterTokens: configuration.filterTokens.filter((token) => {
                if (searchToken && searchToken.values && token && token.values)
                    return !(token.suggestionCategoryId === searchToken.suggestionCategoryId &&
                        token.isFilter === searchToken.isFilter &&
                        token.keywordKey === searchToken.keywordKey &&
                        token.tokenType === searchToken.tokenType &&
                        containsAtLeastOne(searchToken.values, token.values));
                return true;
            }),
        }));
    }, [onConfigurationChange]);
    const resetFilter = React.useCallback(() => {
        selectionsDispatch({ type: "reset-filters" });
        onConfigurationChange(() => ({ filterTokens: [] }));
    }, [onConfigurationChange]);
    const defaultTokens = configuration.defaultTokens;
    return {
        defaultTokens,
        filterTokens,
        addFilterToken,
        removeFilterToken,
        resetFilter,
        setFilterTokens,
    };
}
function useSortResult({ configuration, onConfigurationChange, setSortAfterKey, sortList, }) {
    const sort = configuration.sort;
    const option = sortList?.find((option) => option.isDefault);
    const defaultOption = {
        field: option?.field || "",
        type: option?.type,
    };
    const [selectedSort, setSelectedSort] = React.useState(defaultOption);
    const resetSort = React.useCallback(() => {
        setSortAfterKey("");
        onConfigurationChange(() => ({
            sort: [],
        }));
        setSelectedSort(defaultOption);
    }, [configuration, selectedSort]);
    return { sort, resetSort, selectedSort, setSelectedSort };
}
function useDateTokens() {
    const [dateRange, setDateRange] = React.useState({
        startDate: undefined,
        endDate: undefined,
        keywordKey: undefined,
    });
    const dateTokens = React.useMemo(() => {
        if (dateRange.keywordKey === undefined &&
            dateRange.startDate === undefined &&
            dateRange.endDate === undefined) {
            return [];
        }
        return [
            {
                tokenType: "DATE",
                keywordKey: dateRange.keywordKey,
                extra: {
                    gte: dateRange.startDate?.getTime(),
                    lte: dateRange.endDate?.getTime(),
                },
            },
        ];
    }, [dateRange.endDate, dateRange.keywordKey, dateRange.startDate]);
    return { dateRange, setDateRange, dateTokens };
}
function useDetails(searchQuery, setPrevSearchQuery, prevSearchQuery) {
    const [detail, setDetail] = React.useState(null);
    React.useEffect(() => {
        if (prevSearchQuery !== null && !isEqual(searchQuery, prevSearchQuery)) {
            setDetail(null);
        }
        setPrevSearchQuery(searchQuery);
    }, [searchQuery, prevSearchQuery]);
    return { detail, setDetail };
}
function renderPortal(node, container) {
    const element = typeof container === "string"
        ? document.querySelector(container)
        : container;
    return (_jsx(SimpleErrorBoundary, { children: _jsx(React.Suspense, { children: element ? ReactDOM.createPortal(node, element) : null }) }));
}
function useDetailsMobile(searchQuery, prevSearchQueryMobile, setPrevSearchQueryMobile) {
    const [idPreview, setIdPreview] = React.useState("");
    const [detailMobile, setDetailMobile] = React.useState(null);
    React.useEffect(() => {
        if (prevSearchQueryMobile !== null &&
            !isEqual(searchQuery, prevSearchQueryMobile)) {
            setDetailMobile(null);
        }
        setPrevSearchQueryMobile(searchQuery);
    }, [searchQuery, prevSearchQueryMobile]);
    return { detailMobile, setDetailMobile, idPreview, setIdPreview };
}
function deriveSearchQuery(spans, selection, text, tokenType) {
    return spans
        .map((span) => ({ ...span, text: span.text.trim() }))
        .filter((span) => span.text)
        .map((span) => {
        const token = selection.find((selection) => {
            return selection.start === span.start && selection.end === span.end;
        })?.token ?? null;
        return ((token && analysisTokenToSearchToken(token)) ??
            (text !== undefined
                ? {
                    tokenType,
                    values: [text],
                    filter: false,
                }
                : false));
    });
}
function analysisTokenToSearchToken(token) {
    switch (token.tokenType) {
        case "DATASOURCE":
            return {
                tokenType: "DATASOURCE",
                values: [token.value],
                filter: false,
            };
        case "DOCTYPE":
            return {
                tokenType: "DOCTYPE",
                keywordKey: "type",
                values: [token.value],
                filter: true,
            };
        case "ENTITY":
            return {
                tokenType: "ENTITY",
                keywordKey: token.keywordKey,
                entityType: token.entityType,
                entityName: token.entityName,
                values: [token.value],
                filter: false,
            };
        case "TEXT":
            return {
                tokenType: "TEXT",
                keywordKey: token.keywordKey,
                values: [token.value],
                filter: false,
                extra: token.extra,
            };
        case "FILTER":
            return {
                tokenType: "FILTER",
                keywordKey: token.keywordKey,
                values: [token.value],
                filter: false,
            };
        case "AUTOCOMPLETE":
            return null;
        case "AUTOCORRECT":
            return null;
    }
}
function calculateSpans(text, analysis) {
    const spans = [
        { text: "", start: 0, end: 0, tokens: [] },
    ];
    for (let i = 0; i < text.length;) {
        const found = analysis?.find(({ start, text }) => {
            return i === start && text !== "";
        });
        if (found) {
            spans.push(found);
            i += found.text.length;
            spans.push({ text: "", start: i, end: i, tokens: [] });
        }
        else {
            const last = spans[spans.length - 1];
            last.text += text[i];
            last.end += 1;
            i += 1;
        }
    }
    return spans.filter((span) => span.text);
}
function useQueryAnalysis(request) {
    const client = useOpenK9Client();
    return useQuery(["query-anaylis", request], async ({ queryKey: [, request] }) => fixQueryAnalysisResult(await client.fetchQueryAnalysis(request)));
}
function useQueryAnalysisWithoutSearchData(request) {
    const client = useOpenK9Client();
    return useQuery(["query-anaylis", request], async ({ queryKey: [, request] }) => fixQueryAnalysisResult(await client.fetchQueryAnalysis(request)));
}
// TODO: togliere una volta implementata gestione sugestion sovrapposte
function fixQueryAnalysisResult(data) {
    if (data)
        return {
            ...data,
            analysis: data.analysis
                .reverse()
                .filter((entry, index, array) => array
                .slice(0, index)
                .every((previous) => !isOverlapping(previous, entry)))
                .reverse()
                .filter((entry) => {
                // togliere validazione quando fixato lato be
                const isValidEntry = entry.start >= 0;
                if (!isValidEntry) {
                    console.warn(`Invalid entry: `, entry);
                }
                return isValidEntry;
            }),
        };
}
function createFilter(filterTokens) {
    const groupedTokens = {};
    for (const item of filterTokens) {
        if (item &&
            item.values &&
            item.values.length > 0 &&
            item.suggestionCategoryId) {
            const suggestionCategoryId = item.suggestionCategoryId;
            if (!groupedTokens[suggestionCategoryId]) {
                groupedTokens[suggestionCategoryId] = _.cloneDeep(item);
            }
            else {
                groupedTokens[suggestionCategoryId].values.push(item.values[0]);
            }
        }
    }
    return Object.values(groupedTokens);
}
export function remappingLanguage({ language }) {
    switch (language) {
        case "it_IT":
            return "it";
        case "es_ES":
            return "es";
        case "en_US":
            return "en";
        case "fr_FR":
            return "fr";
        case "de_DE":
            return "de";
        default:
            return "en";
    }
}
function remappingLanguageToBack({ language }) {
    switch (language) {
        case "it":
            return "it_IT";
        case "es":
            return "es_ES";
        case "en":
            return "en_US";
        case "fr":
            return "fr_FR";
        default:
            return "en_US";
    }
}
export const containsAtLeastOne = (array1, array2) => {
    return array1.some((element1) => {
        return array2.includes(element1);
    });
};
function factoryWhoIsDynamic({ whoIsDynamicResponse, }) {
    if (whoIsDynamicResponse === undefined) {
        return [];
    }
    const resultArray = [];
    if (whoIsDynamicResponse.refreshOnQuery) {
        resultArray.push("search");
    }
    if (whoIsDynamicResponse.refreshOnSuggestionCategory) {
        resultArray.push("filter");
    }
    if (whoIsDynamicResponse.refreshOnTab) {
        resultArray.push("tab");
    }
    if (whoIsDynamicResponse.refreshOnDate) {
        resultArray.push("date");
    }
    return resultArray;
}
