import { removeArrayItem } from '../../util/util';
export class MenuController {
    constructor() {
        this._menus = [];
    }
    open(menuId) {
        const menu = this.get(menuId);
        if (menu && !this.isAnimating()) {
            let openedMenu = this.getOpen();
            if (openedMenu && menu !== openedMenu) {
                openedMenu.setOpen(false, false);
            }
            return menu.open();
        }
        return Promise.resolve(false);
    }
    close(menuId) {
        let menu;
        if (menuId) {
            menu = this.get(menuId);
        }
        else {
            menu = this.getOpen();
        }
        if (menu) {
            return menu.close();
        }
        return Promise.resolve(false);
    }
    toggle(menuId) {
        const menu = this.get(menuId);
        if (menu && !this.isAnimating()) {
            var openedMenu = this.getOpen();
            if (openedMenu && menu !== openedMenu) {
                openedMenu.setOpen(false, false);
            }
            return menu.toggle();
        }
        return Promise.resolve(false);
    }
    enable(shouldEnable, menuId) {
        const menu = this.get(menuId);
        if (menu) {
            return menu.enable(shouldEnable);
        }
    }
    swipeEnable(shouldEnable, menuId) {
        const menu = this.get(menuId);
        if (menu) {
            return menu.swipeEnable(shouldEnable);
        }
    }
    isOpen(menuId) {
        if (menuId) {
            var menu = this.get(menuId);
            return menu && menu.isOpen || false;
        }
        else {
            return !!this.getOpen();
        }
    }
    isEnabled(menuId) {
        const menu = this.get(menuId);
        return menu && menu.enabled || false;
    }
    get(menuId) {
        var menu;
        if (menuId === 'left' || menuId === 'right') {
            menu = this._menus.find(m => m.side === menuId && m.enabled);
            if (menu)
                return menu;
            return this._menus.find(m => m.side === menuId) || null;
        }
        else if (menuId) {
            return this._menus.find(m => m.id === menuId) || null;
        }
        menu = this._menus.find(m => m.enabled);
        if (menu) {
            return menu;
        }
        return (this._menus.length ? this._menus[0] : null);
    }
    getOpen() {
        return this._menus.find(m => m.isOpen);
    }
    getMenus() {
        return this._menus;
    }
    isAnimating() {
        return this._menus.some(menu => menu.isAnimating());
    }
    register(menu) {
        this._menus.push(menu);
    }
    unregister(menu) {
        removeArrayItem(this._menus, menu);
    }
    static registerType(name, cls) {
        menuTypes[name] = cls;
    }
    static create(type, menuCmp, platform) {
        return new menuTypes[type](menuCmp, platform);
    }
}
let menuTypes = {};
//# sourceMappingURL=menu-controller.js.map