export function restoreFromParameters(state, action) {
    for (const facetId of Object.keys(state)) {
        delete state[facetId];
    }
    if (action.payload.f) {
        restoreFacets(state, action.payload.f, 'regular');
    }
    if (action.payload.lf) {
        restoreFacets(state, action.payload.lf, 'location');
    }
    if (action.payload.nf) {
        restoreRangeFacets(state, action.payload.nf, 'numericalRange');
    }
    if (action.payload.mnf) {
        restoreManualRangeFacets(state, action.payload.mnf);
    }
    if (action.payload.df) {
        restoreRangeFacets(state, action.payload.df, 'dateRange');
    }
    if (action.payload.cf) {
        restoreCategoryFacets(state, action.payload.cf);
    }
}
function restoreFacets(state, parameterFacets, type) {
    const entries = Object.entries(parameterFacets);
    for (const [facetId, values] of entries) {
        state[facetId] = {
            request: {
                ...restoreFacet(facetId),
                type,
                values: values.map((value) => {
                    const facetValue = {
                        ...restoreFacetValue(),
                        value,
                    };
                    switch (type) {
                        case 'regular':
                            return facetValue;
                        case 'location':
                            return facetValue;
                    }
                }),
            },
        };
    }
}
function restoreRangeFacets(state, parameterFacets, type) {
    const entries = Object.entries(parameterFacets);
    for (const [facetId, values] of entries) {
        state[facetId] = {
            request: {
                ...restoreFacet(facetId),
                type,
                values: values.map((value) => {
                    const rangeValue = {
                        start: value.start,
                        end: value.end,
                        endInclusive: value.endInclusive,
                        ...restoreFacetValue(),
                    };
                    switch (type) {
                        case 'dateRange':
                            return rangeValue;
                        case 'numericalRange':
                            return rangeValue;
                    }
                }),
            },
        };
    }
}
function restoreManualRangeFacets(state, parameterFacets) {
    const entries = Object.entries(parameterFacets);
    for (const [facetId, values] of entries) {
        state[facetId] = {
            request: {
                ...restoreFacet(facetId),
                type: 'numericalRange',
                interval: 'continuous',
                values: values.map((value) => ({
                    start: value.start,
                    end: value.end,
                    endInclusive: value.endInclusive,
                    ...restoreFacetValue(),
                })),
            },
        };
    }
}
function restoreCategoryFacets(state, parameterFacets) {
    const entries = Object.entries(parameterFacets);
    for (const [facetId, path] of entries) {
        state[facetId] = {
            request: {
                ...restoreFacet(facetId),
                type: 'hierarchical',
                values: [],
                // eslint-disable-next-line @cspell/spellchecker
                // TODO CAPI-966: Remove delimitingCharacter
                delimitingCharacter: '|',
                // In the CAPI, the default retrieveCount is 5, while the default
                // numberOfValues is 8. We explicitly set retrieveCount to 8 when
                // restoring category facets to ensure a consistent show more / show
                // less behavior, given that the retrieveCount is not returned in the
                // API response.
                retrieveCount: 8,
            },
        };
        selectPath(state[facetId].request, path);
    }
}
function restoreFacet(facetId) {
    // eslint-disable-next-line @cspell/spellchecker
    // TODO CAPI-966: Only set facet field
    return {
        facetId,
        field: facetId,
        isFieldExpanded: false,
        preventAutoSelect: false,
    };
}
function restoreFacetValue() {
    // eslint-disable-next-line @cspell/spellchecker
    // TODO CAPI-966: Only set facet value state
    return {
        state: 'selected',
        isAutoSelected: false,
        isSuggested: false,
        moreValuesAvailable: true,
    };
}
export function buildSelectedFacetValueRequest(rawValue) {
    return { state: 'selected', value: rawValue };
}
export function selectPath(request, path, initialNumberOfValues) {
    request.values = buildCurrentValuesFromPath(path);
    request.numberOfValues = initialNumberOfValues;
    request.preventAutoSelect = true;
}
export function buildCurrentValuesFromPath(path) {
    if (!path.length) {
        return [];
    }
    const root = buildCategoryFacetValueRequest(path[0]);
    let curr = root;
    const [_first, ...rest] = path;
    for (const segment of rest) {
        const next = buildCategoryFacetValueRequest(segment);
        curr.children.push(next);
        curr = next;
    }
    curr.state = 'selected';
    return [root];
}
export function buildCategoryFacetValueRequest(value) {
    return {
        children: [],
        state: 'idle',
        value,
    };
}
