(function (factory) {
    if (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); if (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", '@angular/core', '@angular/platform-browser', '../../config/config', '../../navigation/nav-util', '../../platform/platform', '../menu/menu-controller'], factory);
    }
})(function (require, exports) {
    "use strict";
    var core_1 = require('@angular/core');
    var platform_browser_1 = require('@angular/platform-browser');
    var config_1 = require('../../config/config');
    var nav_util_1 = require('../../navigation/nav-util');
    var platform_1 = require('../../platform/platform');
    var menu_controller_1 = require('../menu/menu-controller');
    var App = (function () {
        function App(_config, _platform, _menuCtrl) {
            this._config = _config;
            this._platform = _platform;
            this._menuCtrl = _menuCtrl;
            this._disTime = 0;
            this._scrollTime = 0;
            this._title = '';
            this._titleSrv = new platform_browser_1.Title();
            this._rootNav = null;
            this.viewDidLoad = new core_1.EventEmitter();
            this.viewWillEnter = new core_1.EventEmitter();
            this.viewDidEnter = new core_1.EventEmitter();
            this.viewWillLeave = new core_1.EventEmitter();
            this.viewDidLeave = new core_1.EventEmitter();
            this.viewWillUnload = new core_1.EventEmitter();
            _platform.registerBackButtonAction(this.goBack.bind(this));
            this._disableScrollAssist = _config.getBoolean('disableScrollAssist', false);
            (void 0);
        }
        App.prototype.setTitle = function (val) {
            if (val !== this._title) {
                this._title = val;
                this._titleSrv.setTitle(val);
            }
        };
        App.prototype.setElementClass = function (className, isAdd) {
            this._appRoot.setElementClass(className, isAdd);
        };
        App.prototype.setEnabled = function (isEnabled, duration) {
            if (duration === void 0) { duration = 700; }
            this._disTime = (isEnabled ? 0 : Date.now() + duration);
            if (this._clickBlock) {
                if (isEnabled) {
                    this._clickBlock.activate(false, CLICK_BLOCK_BUFFER_IN_MILLIS);
                }
                else {
                    this._clickBlock.activate(true, duration + CLICK_BLOCK_BUFFER_IN_MILLIS);
                }
            }
        };
        App.prototype._setDisableScroll = function (disableScroll) {
            if (this._disableScrollAssist) {
                this._appRoot._disableScroll(disableScroll);
            }
        };
        App.prototype.isEnabled = function () {
            var disTime = this._disTime;
            if (disTime === 0) {
                return true;
            }
            return (disTime < Date.now());
        };
        App.prototype.setScrolling = function (timeStamp) {
            this._scrollTime = timeStamp + ACTIVE_SCROLLING_TIME;
        };
        App.prototype.isScrolling = function () {
            var scrollTime = this._scrollTime;
            if (scrollTime === 0) {
                return false;
            }
            if (scrollTime < Date.now()) {
                this._scrollTime = 0;
                return false;
            }
            return true;
        };
        App.prototype.getActiveNav = function () {
            var portal = this._appRoot._getPortal(MODAL);
            if (portal.length() > 0) {
                return findTopNav(portal);
            }
            return findTopNav(this._rootNav || null);
        };
        App.prototype.getRootNav = function () {
            return this._rootNav;
        };
        App.prototype._setRootNav = function (nav) {
            this._rootNav = nav;
        };
        App.prototype.present = function (enteringView, opts, appPortal) {
            var portal = this._appRoot._getPortal(appPortal);
            enteringView._setNav(portal);
            opts.keyboardClose = false;
            opts.direction = nav_util_1.DIRECTION_FORWARD;
            if (!opts.animation) {
                opts.animation = enteringView.getTransitionName(nav_util_1.DIRECTION_FORWARD);
            }
            enteringView.setLeavingOpts({
                keyboardClose: false,
                direction: nav_util_1.DIRECTION_BACK,
                animation: enteringView.getTransitionName(nav_util_1.DIRECTION_BACK),
                ev: opts.ev
            });
            return portal.insertPages(-1, [enteringView], opts);
        };
        App.prototype.goBack = function () {
            if (this._menuCtrl && this._menuCtrl.isOpen()) {
                return this._menuCtrl.close();
            }
            var navPromise = this.navPop();
            if (navPromise === null) {
                if (this._config.getBoolean('navExitApp', true)) {
                    (void 0);
                    this._platform.exitApp();
                }
            }
            return navPromise;
        };
        App.prototype.navPop = function () {
            if (!this._rootNav || !this.isEnabled()) {
                return Promise.resolve();
            }
            var portal = this._appRoot._getPortal(DEFAULT);
            if (portal.length() > 0) {
                return Promise.resolve();
            }
            return recursivePop(this.getActiveNav());
        };
        App.decorators = [
            { type: core_1.Injectable },
        ];
        App.ctorParameters = [
            { type: config_1.Config, },
            { type: platform_1.Platform, },
            { type: menu_controller_1.MenuController, decorators: [{ type: core_1.Optional },] },
        ];
        return App;
    }());
    exports.App = App;
    function recursivePop(nav) {
        if (!nav) {
            return null;
        }
        if (nav_util_1.isNav(nav)) {
            var len = nav.length();
            if (len > 1 || (nav._isPortal && len > 0)) {
                (void 0);
                return nav.pop();
            }
        }
        return recursivePop(nav.parent);
    }
    function findTopNav(nav) {
        var activeChildNav;
        while (nav) {
            activeChildNav = nav.getActiveChildNav();
            if (!activeChildNav) {
                break;
            }
            nav = activeChildNav;
        }
        return nav;
    }
    var DEFAULT = 0;
    var MODAL = 1;
    var ACTIVE_SCROLLING_TIME = 100;
    var CLICK_BLOCK_BUFFER_IN_MILLIS = 64;
});
//# sourceMappingURL=app.js.map