/**
 * Builder class for creating controllers from definitions
 */
export class ControllerBuilder {
    _definition;
    _engine;
    _props;
    _additionalArgs = [];
    constructor(definition, engine, props) {
        this._definition = definition;
        this._engine = engine;
        this._props = props;
    }
    withAdditionalArgs(additionalArgs = []) {
        this._additionalArgs = additionalArgs;
        return this;
    }
    build() {
        if ('build' in this._definition &&
            typeof this._definition.build === 'function') {
            return this.buildWithoutProps();
        }
        if ('buildWithProps' in this._definition &&
            typeof this._definition.buildWithProps === 'function') {
            return this.buildWithProps();
        }
        throw new Error('Controller definition must have a build or buildWithProps method.');
    }
    buildWithoutProps() {
        const buildFn = this._definition.build;
        return buildFn(this._engine, ...this._additionalArgs);
    }
    buildWithProps() {
        const buildWithPropsFn = this._definition.buildWithProps;
        const controller = buildWithPropsFn(this._engine, this._props, ...this._additionalArgs);
        return { ...controller, initialState: controller.state };
    }
}
