import { buildDidYouMean, } from '../../search/did-you-mean/headless-did-you-mean.js';
import { buildCoreBreadcrumbManager, } from '../breadcrumb-manager/headless-core-breadcrumb-manager.js';
import { buildCategoryFacet } from '../facets/category/headless-commerce-category-facet.js';
import { buildCommerceDateFacet } from '../facets/date/headless-commerce-date-facet.js';
import { buildFacetGenerator, } from '../facets/generator/headless-commerce-facet-generator.js';
import { buildCommerceLocationFacet } from '../facets/location/headless-commerce-location-facet.js';
import { buildCommerceNumericFacet } from '../facets/numeric/headless-commerce-numeric-facet.js';
import { buildCommerceRegularFacet } from '../facets/regular/headless-commerce-regular-facet.js';
import { buildCoreInteractiveProduct, } from '../interactive-product/headless-core-interactive-product.js';
import { buildCorePagination, } from '../pagination/headless-core-commerce-pagination.js';
import { buildCoreParameterManager, } from '../parameter-manager/headless-core-parameter-manager.js';
import { buildCoreSort, } from '../sort/headless-core-commerce-sort.js';
import { buildCoreSummary, } from '../summary/headless-core-summary.js';
import { buildCoreUrlManager, } from '../url-manager/headless-core-url-manager.js';
/**
 * Builds the sub-controllers for the commerce search use case.
 *
 * @param engine - The commerce engine.
 * @param subControllerProps - The properties for the search sub-controllers.
 * @returns The search sub-controllers.
 */
export function buildSearchSubControllers(engine, subControllerProps) {
    return {
        ...buildSearchAndListingsSubControllers(engine, {
            ...subControllerProps,
            facetSearchType: 'SEARCH',
        }),
        didYouMean() {
            return buildDidYouMean(engine);
        },
    };
}
/**
 * Builds the sub-controllers for the commerce product listing use case.
 *
 * @param engine - The commerce engine.
 * @param subControllerProps - The properties for the listing sub-controllers.
 * @returns The product listing sub-controllers.
 */
export function buildProductListingSubControllers(engine, subControllerProps) {
    return buildSearchAndListingsSubControllers(engine, {
        ...subControllerProps,
        facetSearchType: 'LISTING',
    });
}
/**
 * Builds the sub-controllers for the commerce search and product listing use cases.
 *
 * @param engine - The commerce engine.
 * @param subControllerProps - The properties for the search and product listing sub-controllers.
 * @returns The search and product listing sub-controllers.
 */
export function buildSearchAndListingsSubControllers(engine, subControllerProps) {
    const { fetchProductsActionCreator, facetResponseSelector, isFacetLoadingResponseSelector, requestIdSelector, serializer, parametersDefinition, activeParametersSelector, restoreActionCreator, facetSearchType, } = subControllerProps;
    return {
        ...buildBaseSubControllers(engine, subControllerProps),
        sort(props) {
            return buildCoreSort(engine, {
                ...props,
                fetchProductsActionCreator,
            });
        },
        facetGenerator() {
            const commonOptions = {
                fetchProductsActionCreator,
                facetResponseSelector,
                isFacetLoadingResponseSelector,
                facetSearch: { type: facetSearchType },
            };
            return buildFacetGenerator(engine, {
                buildRegularFacet: (_engine, options) => buildCommerceRegularFacet(engine, { ...options, ...commonOptions }),
                buildNumericFacet: (_engine, options) => buildCommerceNumericFacet(engine, { ...options, ...commonOptions }),
                buildDateFacet: (_engine, options) => buildCommerceDateFacet(engine, { ...options, ...commonOptions }),
                buildCategoryFacet: (_engine, options) => buildCategoryFacet(engine, { ...options, ...commonOptions }),
                buildLocationFacet: (_engine, options) => buildCommerceLocationFacet(engine, { ...options, ...commonOptions }),
                fetchProductsActionCreator,
            });
        },
        breadcrumbManager() {
            return buildCoreBreadcrumbManager(engine, {
                facetResponseSelector,
                fetchProductsActionCreator,
            });
        },
        urlManager(props) {
            return buildCoreUrlManager(engine, {
                ...props,
                requestIdSelector,
                parameterManagerBuilder: (_engine, props) => this.parameterManager(props),
                serializer,
            });
        },
        parameterManager(props) {
            return buildCoreParameterManager(engine, {
                ...props,
                parametersDefinition,
                activeParametersSelector,
                restoreActionCreator,
                fetchProductsActionCreator,
            });
        },
    };
}
/**
 * Builds the `InteractiveProduct` and `Pagination` sub-controllers for a commerce engine.
 * @param engine - The commerce engine.
 * @param subControllerProps - The properties for the `InteractiveProduct` and `Pagination` sub-controllers.
 * @returns The `InteractiveProduct` and `Pagination` sub-controllers.
 */
export function buildBaseSubControllers(engine, subControllerProps) {
    const { responseIdSelector, isLoadingSelector, errorSelector, numberOfProductsSelector, fetchProductsActionCreator, fetchMoreProductsActionCreator, slotId, pageSelector, perPageSelector, totalEntriesSelector, enrichSummary, } = subControllerProps;
    return {
        interactiveProduct(props) {
            return buildCoreInteractiveProduct(engine, {
                ...props,
                responseIdSelector,
            });
        },
        pagination(props) {
            return buildCorePagination(engine, {
                ...props,
                options: {
                    ...props?.options,
                    slotId,
                },
                fetchProductsActionCreator,
                fetchMoreProductsActionCreator,
            });
        },
        summary() {
            return buildCoreSummary(engine, {
                options: {
                    responseIdSelector,
                    isLoadingSelector,
                    errorSelector,
                    numberOfProductsSelector,
                    pageSelector,
                    perPageSelector,
                    totalEntriesSelector,
                    enrichSummary,
                },
            });
        },
    };
}
