import { createReducer } from '@reduxjs/toolkit';
import { executeSearch } from '../../../search/search-actions.js';
import { handleFacetSearchClear, handleFacetSearchFulfilled, handleFacetSearchPending, handleFacetSearchRegistration, handleFacetSearchRejected, handleFacetSearchSetClear, handleFacetSearchUpdate, } from '../facet-search-reducer-helpers.js';
import { clearFacetSearch, executeFacetSearch, } from '../generic/generic-facet-search-actions.js';
import { registerFacetSearch, updateFacetSearch, } from './specific-facet-search-actions.js';
import { getFacetSearchSetInitialState } from './specific-facet-search-set-state.js';
export const specificFacetSearchSetReducer = createReducer(getFacetSearchSetInitialState(), (builder) => {
    builder
        .addCase(registerFacetSearch, (state, action) => {
        const payload = action.payload;
        handleFacetSearchRegistration(state, payload, buildEmptyResponse);
    })
        .addCase(updateFacetSearch, (state, action) => {
        handleFacetSearchUpdate(state, action.payload);
    })
        .addCase(executeFacetSearch.pending, (state, action) => {
        const facetId = action.meta.arg;
        handleFacetSearchPending(state, facetId, action.meta.requestId);
    })
        .addCase(executeFacetSearch.rejected, (state, action) => {
        const facetId = action.meta.arg;
        handleFacetSearchRejected(state, facetId);
    })
        .addCase(executeFacetSearch.fulfilled, (state, action) => {
        handleFacetSearchFulfilled(state, action.payload, action.meta.requestId);
    })
        .addCase(clearFacetSearch, (state, { payload }) => {
        handleFacetSearchClear(state, payload, buildEmptyResponse);
    })
        .addCase(executeSearch.fulfilled, (state) => {
        handleFacetSearchSetClear(state, buildEmptyResponse);
    });
});
function buildEmptyResponse() {
    return {
        moreValuesAvailable: false,
        values: [],
    };
}
