import {IFactory} from './interfaces/IFactory';
import {IFactory as IComponentsFactory} from './interfaces/components/IFactory';
import {Factory as ComponentsFactory} from './components/Factory';
import {IFactory as ICommandsFactory} from './interfaces/commands/IFactory';
import {Factory as CommandsFactory} from './commands/Factory';
import {Helpers} from '@common/admin/Helpers';

export class Factory implements IFactory {

    protected conf:any;
    
    protected componentsFactory:IComponentsFactory = null;

    protected commandsFactory:ICommandsFactory = null;

    public init(container:HTMLElement, conf:any = {})
    {
        this.conf = {
            'components': {
                'factory': null,
            },
            'commands': {
                'factory': null,
            },
        };
        Helpers.objectReplaceRecursive(this.conf, conf);
        this.getComponentsFactory().init(container, this.conf.components);
    }
    
    public getComponentsFactory()
    {
        if(this.componentsFactory === null){
            this.componentsFactory = this.conf.components.factory || new ComponentsFactory();
            this.componentsFactory.setAppFactory(this);
        }
        return this.componentsFactory;
    }

    public getCommandsFactory()
    {
        if(this.commandsFactory === null){
            this.commandsFactory = this.conf.commands.factory || new CommandsFactory();
            this.commandsFactory.init(this.conf.commands);
        }
        return this.commandsFactory;
    }

}