import { buildProductListing } from '../../../../controllers/commerce/product-listing/headless-product-listing.js';
import { buildSearch } from '../../../../controllers/commerce/search/headless-search.js';
import { commerceFacetSetReducer as commerceFacetSet } from '../../../../features/commerce/facets/facet-set/facet-set-slice.js';
import { manualNumericFacetReducer as manualNumericFacetSet } from '../../../../features/commerce/facets/numeric-facet/manual-numeric-facet-slice.js';
import { paginationReducer as commercePagination } from '../../../../features/commerce/pagination/pagination-slice.js';
import { parametersReducer as commerceParameters } from '../../../../features/commerce/parameters/parameters-slice.js';
import { queryReducer as query } from '../../../../features/commerce/query/query-slice.js';
import { sortReducer as commerceSort } from '../../../../features/commerce/sort/sort-slice.js';
import { facetOrderReducer as facetOrder } from '../../../../features/facets/facet-order/facet-order-slice.js';
import { querySetReducer as querySet } from '../../../../features/query-set/query-set-slice.js';
import { ensureAtLeastOneSolutionType } from '../../../../ssr/commerce/controller-utils.js';
import { loadReducerError } from '../../../../utils/errors.js';
import { SolutionType } from '../../types/controller-constants.js';
import { createControllerWithKind, Kind } from '../../types/kind.js';
/**
 * Defines a `ParameterManager` controller instance.
 * @group Definers
 *
 * @returns The `ParameterManager` controller definition.
 */
export function defineParameterManager(options) {
    ensureAtLeastOneSolutionType(options);
    return {
        listing: true,
        search: true,
        ...options,
        buildWithProps: (engine, props, solutionType) => {
            if (solutionType === SolutionType.listing) {
                if (!loadCommerceProductListingParameterReducers(engine)) {
                    throw loadReducerError;
                }
                const controller = buildProductListing(engine).parameterManager({
                    ...props,
                    excludeDefaultParameters: true,
                });
                return createControllerWithKind(controller, Kind.ParameterManager);
            }
            else {
                if (!loadCommerceSearchParameterReducers(engine)) {
                    throw loadReducerError;
                }
                const controller = buildSearch(engine).parameterManager({
                    ...props,
                    excludeDefaultParameters: true,
                });
                return createControllerWithKind(controller, Kind.ParameterManager);
            }
        },
    };
}
function loadCommerceCommonParameterReducers(engine) {
    engine.addReducers({
        commerceParameters,
        commerceFacetSet,
        commerceSort,
        commercePagination,
        facetOrder,
        manualNumericFacetSet,
    });
    return true;
}
function loadCommerceSearchParameterReducers(engine) {
    loadCommerceCommonParameterReducers(engine);
    engine.addReducers({ query, querySet });
    return true;
}
function loadCommerceProductListingParameterReducers(engine) {
    loadCommerceCommonParameterReducers(engine);
    return true;
}
