import { InvalidControllerDefinition } from '../../utils/errors.js';
import { clone, filterObject, mapObject } from '../../utils/utils.js';
function hasKindProperty(controller) {
    return '_kind' in controller;
}
export function createStaticState({ searchActions, controllers, }) {
    return {
        controllers: mapObject(controllers, (controller) => ({
            state: clone(controller.state),
            ...(hasKindProperty(controller) && { _kind: controller._kind }),
        })),
        searchActions: searchActions.map((action) => clone(action)),
    };
}
function buildControllerFromDefinition({ definition, engine, solutionType, props, }) {
    return ('build' in definition
        ? definition.build(engine, solutionType)
        : definition.buildWithProps(engine, props, solutionType));
}
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 buildControllerFromDefinition({
            definition,
            engine,
            solutionType,
            props,
        });
    });
    return filterObject(controllerMap, (value) => value !== null);
}
export function ensureAtLeastOneSolutionType(options) {
    if (options?.listing === false && options?.search === false) {
        throw new InvalidControllerDefinition();
    }
}
