import { clone, filterObject, mapObject } from '../../utils/utils.js';
import { ControllerBuilder } from '../common/builders/controller-builder.js';
import { createStaticControllerBuilder } from '../common/builders/static-controller-builder.js';
import { InvalidControllerDefinition } from '../common/errors.js';
export function createStaticState({ searchActions, controllers, }) {
    return {
        controllers: mapObject(controllers, (controller) => createStaticControllerBuilder(controller).build()),
        searchActions: searchActions.map((action) => clone(action)),
    };
}
export function buildControllerDefinitions({ definitionsMap, engine, solutionType, propsMap, }) {
    const controllerMap = mapObject(definitionsMap, (definition, key) => {
        const unavailableInSolutionType = () => !(solutionType in definition) ||
            (solutionType in definition &&
                definition[solutionType] === false);
        const props = propsMap?.[key];
        if (unavailableInSolutionType()) {
            return null;
        }
        return new ControllerBuilder(definition, engine, props)
            .withAdditionalArgs([solutionType])
            .build();
    });
    return filterObject(controllerMap, (value) => value !== null);
}
export function ensureAtLeastOneSolutionType(options) {
    if (options?.listing === false && options?.search === false) {
        throw new InvalidControllerDefinition();
    }
}
