import { facetClearAll, facetDeselect, facetSelect, } from '../../../../features/facets/facet-set/facet-set-analytics-actions.js';
import { logFacetClearAll, logFacetDeselect, logFacetSelect, logFacetShowLess, logFacetShowMore, logFacetUpdateSort, } from '../../../../features/facets/facet-set/facet-set-insight-analytics-actions.js';
import { executeSearch, fetchFacetValues, } from '../../../../features/insight-search/insight-search-actions.js';
import { buildCoreCategoryFacet, } from '../../../core/facets/category-facet/headless-core-category-facet.js';
/**
 * Creates an insight `CategoryFacet` controller instance.
 *
 * @param engine - The insight engine.
 * @param props - The configurable `CategoryFacet` properties.
 * @returns A `CategoryFacet` controller instance.
 *
 * @group Controllers
 * @category CategoryFacet
 * */
export function buildCategoryFacet(engine, props) {
    const coreController = buildCoreCategoryFacet(engine, props);
    const { dispatch } = engine;
    const getFacetId = () => coreController.state.facetId;
    const createNoopCategoryFacetSearch = () => {
        return {
            updateText() { },
            showMoreResults() { },
            search() { },
            clear() { },
            updateCaptions() { },
            select() { },
            singleSelect() { },
            get state() {
                return {
                    values: [],
                    isLoading: false,
                    moreValuesAvailable: false,
                    query: '',
                };
            },
        };
    };
    const facetSearch = createNoopCategoryFacetSearch();
    const { state, ...restOfFacetSearch } = facetSearch;
    return {
        ...coreController,
        facetSearch: restOfFacetSearch,
        toggleSelect(selection) {
            coreController.toggleSelect(selection);
            const analyticsAction = getToggleSelectInsightAnalyticsAction(getFacetId(), selection);
            dispatch(executeSearch({
                legacy: analyticsAction,
                next: getToggleSelectAnalyticsAction(selection),
            }));
        },
        deselectAll() {
            coreController.deselectAll();
            dispatch(executeSearch({
                legacy: logFacetClearAll(getFacetId()),
                next: facetClearAll(),
            }));
        },
        sortBy(criterion) {
            coreController.sortBy(criterion);
            dispatch(executeSearch({
                legacy: logFacetUpdateSort({
                    facetId: getFacetId(),
                    sortCriterion: criterion,
                }),
            }));
        },
        isSortedBy(criterion) {
            return coreController.isSortedBy(criterion);
        },
        showMoreValues() {
            coreController.showMoreValues();
            dispatch(fetchFacetValues({
                legacy: logFacetShowMore(getFacetId()),
            }));
        },
        showLessValues() {
            coreController.showLessValues();
            dispatch(fetchFacetValues({
                legacy: logFacetShowLess(getFacetId()),
            }));
        },
        get state() {
            return {
                ...coreController.state,
                facetSearch: facetSearch.state,
            };
        },
    };
}
function getToggleSelectInsightAnalyticsAction(facetId, selection) {
    const payload = {
        facetId,
        facetValue: selection.value,
    };
    const isSelected = selection.state === 'selected';
    return isSelected ? logFacetDeselect(payload) : logFacetSelect(payload);
}
export function getToggleSelectAnalyticsAction(selection) {
    const isSelected = selection.state === 'selected';
    return isSelected ? facetDeselect() : facetSelect();
}
