import { EventEmitter, Injectable, Optional } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { Config } from '../../config/config';
import { isNav, DIRECTION_FORWARD, DIRECTION_BACK } from '../../navigation/nav-util';
import { Platform } from '../../platform/platform';
import { MenuController } from '../menu/menu-controller';
export 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 Title();
        this._rootNav = null;
        this.viewDidLoad = new EventEmitter();
        this.viewWillEnter = new EventEmitter();
        this.viewDidEnter = new EventEmitter();
        this.viewWillLeave = new EventEmitter();
        this.viewDidLeave = new EventEmitter();
        this.viewWillUnload = new 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 = DIRECTION_FORWARD;
        if (!opts.animation) {
            opts.animation = enteringView.getTransitionName(DIRECTION_FORWARD);
        }
        enteringView.setLeavingOpts({
            keyboardClose: false,
            direction: DIRECTION_BACK,
            animation: enteringView.getTransitionName(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: Injectable },
    ];
    App.ctorParameters = [
        { type: Config, },
        { type: Platform, },
        { type: MenuController, decorators: [{ type: Optional },] },
    ];
    return App;
}());
function recursivePop(nav) {
    if (!nav) {
        return null;
    }
    if (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