import { buildProductListing } from '../../../controllers/commerce/product-listing/headless-product-listing.js';
import { buildSearch } from '../../../controllers/commerce/search/headless-search.js';
import { augmentPreprocessRequestWithForwardedFor } from '../../common/augment-preprocess-request.js';
import { createStaticState } from '../controller-utils.js';
import { SolutionType } from '../types/controller-constants.js';
import { buildFactory, } from './build-factory.js';
export function fetchStaticStateFactory(controllerDefinitions, options) {
    return (solutionType) => async (...params) => {
        const solutionTypeBuild = await buildFactory(controllerDefinitions, options)(solutionType);
        const { engine, controllers } = await solutionTypeBuild(...params);
        options.configuration.preprocessRequest =
            augmentPreprocessRequestWithForwardedFor({
                preprocessRequest: options.configuration.preprocessRequest,
                navigatorContextProvider: options.navigatorContextProvider,
                loggerOptions: options.loggerOptions,
            });
        switch (solutionType) {
            case SolutionType.listing:
                buildProductListing(engine).executeFirstRequest();
                break;
            case SolutionType.search:
                buildSearch(engine).executeFirstSearch();
                break;
        }
        const searchActions = await Promise.all(engine.waitForRequestCompletedAction());
        const staticState = createStaticState({
            searchActions,
            controllers,
        });
        return staticState;
    };
}
