export function handleFacetSearchRegistration(state, payload, buildEmptyResponse) {
    const { facetId } = payload;
    if (state[facetId]) {
        return;
    }
    const isLoading = false;
    const options = { ...defaultFacetSearchOptions, ...payload };
    const response = buildEmptyResponse();
    state[facetId] = {
        options,
        isLoading,
        response,
        initialNumberOfValues: options.numberOfValues,
        requestId: '',
    };
}
export function handleFacetSearchUpdate(state, payload) {
    const { facetId, ...rest } = payload;
    const search = state[facetId];
    if (!search) {
        return;
    }
    search.options = { ...search.options, ...rest };
}
export function handleFacetSearchPending(state, facetId, requestId) {
    const search = state[facetId];
    if (!search) {
        return;
    }
    search.requestId = requestId;
    search.isLoading = true;
}
export function handleFacetSearchRejected(state, facetId) {
    const search = state[facetId];
    if (!search) {
        return;
    }
    search.isLoading = false;
}
export function handleFacetSearchFulfilled(state, payload, requestId) {
    const { facetId, response } = payload;
    const search = state[facetId];
    if (!search) {
        return;
    }
    if (search.requestId !== requestId) {
        return;
    }
    search.isLoading = false;
    search.response = response;
}
export function handleFacetSearchClear(state, payload, buildEmptyResponse) {
    const { facetId } = payload;
    const search = state[facetId];
    if (!search) {
        return;
    }
    search.requestId = '';
    search.isLoading = false;
    search.response = buildEmptyResponse();
    search.options.numberOfValues = search.initialNumberOfValues;
    search.options.query = defaultFacetSearchOptions.query;
}
export function handleFacetSearchSetClear(state, buildEmptyResponse) {
    Object.keys(state).forEach((facetId) => handleFacetSearchClear(state, { facetId }, buildEmptyResponse));
}
export const defaultFacetSearchOptions = {
    captions: {},
    numberOfValues: 10,
    query: '',
};
