import { createReducer } from '@reduxjs/toolkit';
import { deselectAllBreadcrumbs } from '../../../breadcrumb/breadcrumb-actions.js';
import { disableFacet } from '../../../facet-options/facet-options-actions.js';
import { isFacetVisibleOnTab } from '../../../facet-options/facet-options-utils.js';
import { change } from '../../../history/history-actions.js';
import { executeSearch } from '../../../search/search-actions.js';
import { restoreSearchParameters } from '../../../search-parameters/search-parameter-actions.js';
import { updateActiveTab } from '../../../tab-set/tab-set-actions.js';
import { handleFacetSortCriterionUpdate } from '../../generic/facet-reducer-helpers.js';
import { defaultRangeFacetOptions, handleRangeFacetDeselectAll, handleRangeFacetSearchParameterRestoration, onRangeFacetRequestFulfilled, registerRangeFacet, toggleExcludeRangeValue, toggleSelectRangeValue, updateRangeValues, } from '../generic/range-facet-reducers.js';
import { deselectAllNumericFacetValues, registerNumericFacet, toggleExcludeNumericFacetValue, toggleSelectNumericFacetValue, updateNumericFacetSortCriterion, updateNumericFacetValues, } from './numeric-facet-actions.js';
import { getNumericFacetSetInitialState, getNumericFacetSetSliceInitialState, } from './numeric-facet-set-state.js';
export const numericFacetSetReducer = createReducer(getNumericFacetSetInitialState(), (builder) => {
    builder
        .addCase(registerNumericFacet, (state, action) => {
        const { payload } = action;
        const { tabs } = payload;
        const request = buildNumericFacetRequest(payload);
        registerRangeFacet(state, getNumericFacetSetSliceInitialState(request, tabs));
    })
        .addCase(change.fulfilled, (state, action) => action.payload?.numericFacetSet ?? state)
        .addCase(restoreSearchParameters, (state, action) => {
        const nf = action.payload.nf || {};
        handleRangeFacetSearchParameterRestoration(state, nf);
    })
        .addCase(toggleSelectNumericFacetValue, (state, action) => {
        const { facetId, selection } = action.payload;
        toggleSelectRangeValue(state, facetId, selection);
    })
        .addCase(toggleExcludeNumericFacetValue, (state, action) => {
        const { facetId, selection } = action.payload;
        toggleExcludeRangeValue(state, facetId, selection);
    })
        .addCase(updateNumericFacetValues, (state, action) => {
        const { facetId, values } = action.payload;
        updateRangeValues(state, facetId, values);
    })
        .addCase(deselectAllNumericFacetValues, (state, action) => {
        handleRangeFacetDeselectAll(state, action.payload);
    })
        .addCase(deselectAllBreadcrumbs, (state) => {
        Object.keys(state).forEach((facetId) => {
            handleRangeFacetDeselectAll(state, facetId);
        });
    })
        .addCase(updateNumericFacetSortCriterion, (state, action) => {
        handleFacetSortCriterionUpdate(state, action.payload);
    })
        .addCase(executeSearch.fulfilled, (state, action) => {
        const facets = action.payload.response.facets;
        onRangeFacetRequestFulfilled(state, facets, convertToNumericRangeRequests);
    })
        .addCase(disableFacet, (state, action) => {
        handleRangeFacetDeselectAll(state, action.payload);
    })
        .addCase(updateActiveTab, (state, action) => {
        const newActiveTab = action.payload;
        Object.keys(state).forEach((facetId) => {
            const facetSlice = state[facetId];
            const hasTabs = facetSlice.tabs?.included?.length ||
                facetSlice.tabs?.excluded?.length;
            if (hasTabs && !isFacetVisibleOnTab(facetSlice.tabs, newActiveTab)) {
                handleRangeFacetDeselectAll(state, facetId);
            }
        });
    });
});
function buildNumericFacetRequest(config) {
    return {
        ...defaultRangeFacetOptions,
        currentValues: [],
        preventAutoSelect: false,
        type: 'numericalRange',
        ...config,
    };
}
export function convertToNumericRangeRequests(values) {
    return values.map((value) => {
        const { numberOfResults: _numberOfResults, ...rest } = value;
        return rest;
    });
}
