import { composeFunction } from '../../ssr-engine/common.js';
import { buildFactory, } from './build-factory.js';
export const hydratedStaticStateFactory = (controllerDefinitions, options) => (solutionType) => composeFunction(async (...params) => {
    const solutionTypeBuild = await buildFactory(controllerDefinitions, {
        ...options,
    })(solutionType);
    const buildResult = await solutionTypeBuild(...params);
    const staticStateBuild = await hydratedStaticStateFactory(controllerDefinitions, options)(solutionType);
    const staticState = await staticStateBuild.fromBuildResult({
        buildResult,
        searchActions: params[0].searchActions,
    });
    return staticState;
}, {
    fromBuildResult: async (...params) => {
        const [{ buildResult: { engine, controllers }, searchActions, },] = params;
        searchActions.forEach((action) => {
            engine.dispatch(action);
        });
        await engine.waitForRequestCompletedAction();
        return { engine, controllers };
    },
});
