import { findActiveValueAncestry } from '../../facets/category-facet-set/category-facet-utils.js';
import { getFacets, getSortCriteria, } from '../../parameter-manager/parameter-manager-selectors.js';
import { getCommercePaginationInitialSlice, } from '../pagination/pagination-state.js';
import { getCommerceSortInitialState } from '../sort/sort-state.js';
export function activeParametersSelector(state) {
    return {
        ...getPage(state?.commercePagination, (s) => s.principal.page, getCommercePaginationInitialSlice().page),
        ...getPerPage(state?.commercePagination, (s) => s.principal.perPage, getCommercePaginationInitialSlice().perPage),
        ...getSortCriteria(state?.commerceSort, (s) => s.appliedSort, getCommerceSortInitialState().appliedSort),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'regular'), getSelectedValues, 'f'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'regular'), getExcludedValues, 'fExcluded'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'location'), getSelectedLocationValues, 'lf'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'hierarchical'), getSelectedCategoryValues, 'cf'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'dateRange'), getSelectedRangeValues, 'df'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'dateRange'), getExcludedRangeValues, 'dfExcluded'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'numericalRange'), getSelectedRangeValues, 'nf'),
        ...getFacets(state.commerceFacetSet, facetIsOfType(state, 'numericalRange'), getExcludedRangeValues, 'nfExcluded'),
        ...getManualNumericFacet('selected', state.manualNumericFacetSet),
        ...getManualNumericFacet('excluded', state.manualNumericFacetSet),
    };
}
function getPage(section, pageSelector, initialState) {
    if (section === undefined) {
        return {};
    }
    const page = pageSelector(section);
    const shouldInclude = page !== initialState;
    return shouldInclude ? { page } : {};
}
function getPerPage(section, perPageSelector, initialState) {
    if (section === undefined) {
        return {};
    }
    const perPage = perPageSelector(section);
    const shouldInclude = perPage !== initialState;
    return shouldInclude ? { perPage } : {};
}
function getSelectedValues(request) {
    return request.values
        .filter((fv) => fv.state === 'selected')
        .map((fv) => fv.value);
}
function getExcludedValues(request) {
    return request.values
        .filter((fv) => fv.state === 'excluded')
        .map((fv) => fv.value);
}
function getSelectedLocationValues(request) {
    return request.values
        .filter((fv) => fv.state === 'selected')
        .map((fv) => fv.value);
}
function getSelectedRangeValues(request) {
    return request.values.filter((fv) => fv.state === 'selected');
}
function getExcludedRangeValues(request) {
    return request.values.filter((fv) => fv.state === 'excluded');
}
function getManualNumericFacet(state, section) {
    if (!section || state === 'idle') {
        return {};
    }
    const manualNumericFacets = Object.entries(section)
        .map(([facetId, manualFacetRange]) => {
        if (manualFacetRange.manualRange === undefined ||
            manualFacetRange.manualRange.state !== state) {
            return;
        }
        return { [facetId]: [manualFacetRange.manualRange] };
    })
        .filter((manualRange) => manualRange !== undefined)
        // oxlint-disable-next-line oxc/no-accumulating-spread -- <>
        .reduce((acc, obj) => ({ ...acc, ...obj }), {});
    return state === 'selected'
        ? { mnf: manualNumericFacets }
        : { mnfExcluded: manualNumericFacets };
}
function getSelectedCategoryValues(request) {
    const categoryRequest = request;
    return findActiveValueAncestry(categoryRequest.values).map((v) => v.value);
}
function facetIsOfType(state, type) {
    return (facetId) => {
        return state.commerceFacetSet[facetId].request.type === type;
    };
}
