import { createReducer } from '@reduxjs/toolkit';
import { setError } from '../error/error-actions.js';
import { executeSearch, fetchFacetValues, fetchMoreResults, fetchPage, } from './legacy/search-actions.js';
import { updateSearchAction } from './search-actions.js';
import { emptyQuestionAnswer, getSearchInitialState, } from './search-state.js';
function handleRejectedSearch(state, action) {
    const error = action.payload ?? null;
    if (error) {
        state.response = getSearchInitialState().response;
        state.results = [];
        state.questionAnswer = emptyQuestionAnswer();
    }
    state.error = error;
    state.isLoading = false;
}
function handleFulfilledSearch(state, action) {
    state.error = null;
    state.response = action.payload.response;
    state.queryExecuted = action.payload.queryExecuted;
    state.duration = action.payload.duration;
    state.isLoading = false;
}
function handleFulfilledNewSearch(state, action) {
    handleFulfilledSearch(state, action);
    state.results = action.payload.response.results.map((result) => ({
        ...result,
        searchUid: action.payload.response.searchUid,
    }));
    state.searchResponseId = action.payload.response.searchUid;
    state.questionAnswer = action.payload.response.questionAnswer;
    state.extendedResults = action.payload.response.extendedResults;
}
function handlePendingSearch(state, action) {
    state.isLoading = true;
    state.requestId = action.meta.requestId;
}
export const searchReducer = createReducer(getSearchInitialState(), (builder) => {
    builder.addCase(executeSearch.rejected, (state, action) => handleRejectedSearch(state, action));
    builder.addCase(fetchMoreResults.rejected, (state, action) => handleRejectedSearch(state, action));
    builder.addCase(fetchPage.rejected, (state, action) => handleRejectedSearch(state, action));
    builder.addCase(executeSearch.fulfilled, (state, action) => {
        handleFulfilledNewSearch(state, action);
    });
    builder.addCase(fetchMoreResults.fulfilled, (state, action) => {
        handleFulfilledSearch(state, action);
        state.results = [
            ...state.results,
            ...action.payload.response.results.map((result) => ({
                ...result,
                searchUid: action.payload.response.searchUid,
            })),
        ];
    });
    builder.addCase(fetchPage.fulfilled, (state, action) => {
        handleFulfilledSearch(state, action);
        state.results = [
            ...action.payload.response.results.map((result) => ({
                ...result,
                searchUid: action.payload.response.searchUid,
            })),
        ];
    });
    builder.addCase(fetchFacetValues.fulfilled, (state, action) => {
        state.response.facets = action.payload.response.facets;
        state.response.searchUid = action.payload.response.searchUid;
    });
    builder.addCase(executeSearch.pending, handlePendingSearch);
    builder.addCase(fetchMoreResults.pending, handlePendingSearch);
    builder.addCase(fetchPage.pending, handlePendingSearch);
    builder.addCase(updateSearchAction, (state, action) => {
        state.searchAction = action.payload;
    });
    builder.addCase(setError, (state, action) => {
        state.error = action.payload;
        state.isLoading = false;
    });
});
