"use strict";
var core_1 = require('@angular/core');
var backdrop_1 = require('../backdrop/backdrop');
var config_1 = require('../../config/config');
var util_1 = require('../../util/util');
var keyboard_1 = require('../../util/keyboard');
var menu_gestures_1 = require('./menu-gestures');
var menu_controller_1 = require('./menu-controller');
var platform_1 = require('../../platform/platform');
var gesture_controller_1 = require('../../gestures/gesture-controller');
var Menu = (function () {
    function Menu(_menuCtrl, _elementRef, _config, _platform, _renderer, _keyboard, _zone, gestureCtrl) {
        this._elementRef = _elementRef;
        this._config = _config;
        this._platform = _platform;
        this._renderer = _renderer;
        this._keyboard = _keyboard;
        this._zone = _zone;
        this.gestureCtrl = gestureCtrl;
        this._preventTime = 0;
        this._isEnabled = true;
        this._isSwipeEnabled = true;
        this._isPers = false;
        this._init = false;
        this.isOpen = false;
        this.ionDrag = new core_1.EventEmitter();
        this.ionOpen = new core_1.EventEmitter();
        this.ionClose = new core_1.EventEmitter();
        this._menuCtrl = _menuCtrl;
    }
    Object.defineProperty(Menu.prototype, "enabled", {
        get: function () {
            return this._isEnabled;
        },
        set: function (val) {
            this._isEnabled = util_1.isTrueProperty(val);
            this._setListeners();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Menu.prototype, "swipeEnabled", {
        get: function () {
            return this._isSwipeEnabled;
        },
        set: function (val) {
            this._isSwipeEnabled = util_1.isTrueProperty(val);
            this._setListeners();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Menu.prototype, "persistent", {
        get: function () {
            return this._isPers;
        },
        set: function (val) {
            this._isPers = util_1.isTrueProperty(val);
        },
        enumerable: true,
        configurable: true
    });
    Menu.prototype.ngOnInit = function () {
        var self = this;
        self._init = true;
        var content = self.content;
        self._cntEle = (content instanceof Node) ? content : content && content.getNativeElement && content.getNativeElement();
        if (!self._cntEle) {
            return console.error('Menu: must have a [content] element to listen for drag events on. Example:\n\n<ion-menu [content]="content"></ion-menu>\n\n<ion-nav #content></ion-nav>');
        }
        if (self.side !== 'left' && self.side !== 'right') {
            self.side = 'left';
        }
        self._renderer.setElementAttribute(self._elementRef.nativeElement, 'side', self.side);
        if (!self.type) {
            self.type = self._config.get('menuType');
        }
        self._renderer.setElementAttribute(self._elementRef.nativeElement, 'type', self.type);
        self._cntGesture = new menu_gestures_1.MenuContentGesture(self, document.body);
        var hasEnabledSameSideMenu = self._menuCtrl.getMenus().some(function (m) {
            return m.side === self.side && m.enabled;
        });
        if (hasEnabledSameSideMenu) {
            self._isEnabled = false;
        }
        self._setListeners();
        self.onContentClick = function (ev) {
            if (self._isEnabled) {
                ev.preventDefault();
                ev.stopPropagation();
                self.close();
            }
        };
        self._cntEle.classList.add('menu-content');
        self._cntEle.classList.add('menu-content-' + self.type);
        self._menuCtrl.register(self);
    };
    Menu.prototype.bdClick = function (ev) {
        console.debug('backdrop clicked');
        ev.preventDefault();
        ev.stopPropagation();
        this._menuCtrl.close();
    };
    Menu.prototype._setListeners = function () {
        var self = this;
        if (self._init) {
            if (self._isEnabled && self._isSwipeEnabled && !self._cntGesture.isListening) {
                console.debug('menu, gesture listen', self.side);
                self._cntGesture.listen();
            }
            else if (self._cntGesture.isListening && (!self._isEnabled || !self._isSwipeEnabled)) {
                console.debug('menu, gesture unlisten', self.side);
                self._cntGesture.unlisten();
            }
        }
    };
    Menu.prototype._getType = function () {
        if (!this._type) {
            this._type = menu_controller_1.MenuController.create(this.type, this, this._platform);
            if (this._config.get('animate') === false) {
                this._type.ani.duration(0);
            }
        }
        return this._type;
    };
    Menu.prototype.setOpen = function (shouldOpen, animated) {
        var _this = this;
        if (animated === void 0) { animated = true; }
        if ((shouldOpen && this.isOpen) || this._isPrevented()) {
            return Promise.resolve(this.isOpen);
        }
        this._before();
        return new Promise(function (resolve) {
            _this._getType().setOpen(shouldOpen, animated, function () {
                _this._after(shouldOpen);
                resolve(_this.isOpen);
            });
        });
    };
    Menu.prototype.swipeStart = function () {
        if (this._isEnabled && this._isSwipeEnabled && !this._isPrevented()) {
            this._before();
            this._getType().setProgressStart(this.isOpen);
        }
    };
    Menu.prototype.swipeProgress = function (stepValue) {
        if (this._isEnabled && this._isSwipeEnabled) {
            this._prevent();
            this._getType().setProgessStep(stepValue);
            this.ionDrag.emit(stepValue);
        }
    };
    Menu.prototype.swipeEnd = function (shouldCompleteLeft, shouldCompleteRight, stepValue) {
        var _this = this;
        if (this._isEnabled && this._isSwipeEnabled) {
            this._prevent();
            var opening = !this.isOpen;
            var shouldComplete = false;
            if (opening) {
                shouldComplete = (this.side === 'right') ? shouldCompleteLeft : shouldCompleteRight;
            }
            else {
                shouldComplete = (this.side === 'right') ? shouldCompleteRight : shouldCompleteLeft;
            }
            this._getType().setProgressEnd(shouldComplete, stepValue, function (isOpen) {
                console.debug('menu, swipeEnd', _this.side);
                _this._after(isOpen);
            });
        }
    };
    Menu.prototype._before = function () {
        if (this._isEnabled) {
            this.getNativeElement().classList.add('show-menu');
            this.getBackdropElement().classList.add('show-backdrop');
            this._prevent();
            this._keyboard.close();
        }
    };
    Menu.prototype._after = function (isOpen) {
        if ((this._isEnabled && isOpen) || !isOpen) {
            this._prevent();
            this.isOpen = isOpen;
            this._cntEle.classList[isOpen ? 'add' : 'remove']('menu-content-open');
            this._cntEle.removeEventListener('click', this.onContentClick);
            if (isOpen) {
                this._cntEle.addEventListener('click', this.onContentClick);
                this.ionOpen.emit(true);
            }
            else {
                this.getNativeElement().classList.remove('show-menu');
                this.getBackdropElement().classList.remove('show-backdrop');
                this.ionClose.emit(true);
            }
        }
    };
    Menu.prototype._prevent = function () {
        this._preventTime = Date.now() + 20;
    };
    Menu.prototype._isPrevented = function () {
        return this._preventTime > Date.now();
    };
    Menu.prototype.open = function () {
        return this.setOpen(true);
    };
    Menu.prototype.close = function () {
        return this.setOpen(false);
    };
    Menu.prototype.toggle = function () {
        return this.setOpen(!this.isOpen);
    };
    Menu.prototype.enable = function (shouldEnable) {
        var _this = this;
        this.enabled = shouldEnable;
        if (!shouldEnable && this.isOpen) {
            this.close();
        }
        if (shouldEnable) {
            this._menuCtrl.getMenus()
                .filter(function (m) { return m.side === _this.side && m !== _this; })
                .map(function (m) { return m.enabled = false; });
        }
        return this;
    };
    Menu.prototype.swipeEnable = function (shouldEnable) {
        this.swipeEnabled = shouldEnable;
        return this;
    };
    Menu.prototype.getNativeElement = function () {
        return this._elementRef.nativeElement;
    };
    Menu.prototype.getMenuElement = function () {
        return this.getNativeElement().querySelector('.menu-inner');
    };
    Menu.prototype.getContentElement = function () {
        return this._cntEle;
    };
    Menu.prototype.getBackdropElement = function () {
        return this.backdrop.getNativeElement();
    };
    Menu.prototype.width = function () {
        return this.getMenuElement().offsetWidth;
    };
    Menu.prototype.getMenuController = function () {
        return this._menuCtrl;
    };
    Menu.prototype.ngOnDestroy = function () {
        this._menuCtrl.unregister(this);
        this._cntGesture && this._cntGesture.destroy();
        this._type && this._type.destroy();
        this._resizeUnreg && this._resizeUnreg();
        this._cntEle = null;
    };
    Menu.decorators = [
        { type: core_1.Component, args: [{
                    selector: 'ion-menu',
                    template: '<div class="menu-inner"><ng-content></ng-content></div>' +
                        '<ion-backdrop (click)="bdClick($event)" disableScroll="false"></ion-backdrop>',
                    host: {
                        'role': 'navigation'
                    },
                    changeDetection: core_1.ChangeDetectionStrategy.OnPush,
                    encapsulation: core_1.ViewEncapsulation.None,
                },] },
    ];
    Menu.ctorParameters = [
        { type: menu_controller_1.MenuController, },
        { type: core_1.ElementRef, },
        { type: config_1.Config, },
        { type: platform_1.Platform, },
        { type: core_1.Renderer, },
        { type: keyboard_1.Keyboard, },
        { type: core_1.NgZone, },
        { type: gesture_controller_1.GestureController, },
    ];
    Menu.propDecorators = {
        'backdrop': [{ type: core_1.ViewChild, args: [backdrop_1.Backdrop,] },],
        'content': [{ type: core_1.Input },],
        'id': [{ type: core_1.Input },],
        'side': [{ type: core_1.Input },],
        'type': [{ type: core_1.Input },],
        'enabled': [{ type: core_1.Input },],
        'swipeEnabled': [{ type: core_1.Input },],
        'persistent': [{ type: core_1.Input },],
        'maxEdgeStart': [{ type: core_1.Input },],
        'ionDrag': [{ type: core_1.Output },],
        'ionOpen': [{ type: core_1.Output },],
        'ionClose': [{ type: core_1.Output },],
    };
    return Menu;
}());
exports.Menu = Menu;
