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 class App {
    constructor(_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);
    }
    setTitle(val) {
        if (val !== this._title) {
            this._title = val;
            this._titleSrv.setTitle(val);
        }
    }
    setElementClass(className, isAdd) {
        this._appRoot.setElementClass(className, isAdd);
    }
    setEnabled(isEnabled, 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);
            }
        }
    }
    _setDisableScroll(disableScroll) {
        if (this._disableScrollAssist) {
            this._appRoot._disableScroll(disableScroll);
        }
    }
    isEnabled() {
        const disTime = this._disTime;
        if (disTime === 0) {
            return true;
        }
        return (disTime < Date.now());
    }
    setScrolling(timeStamp) {
        this._scrollTime = timeStamp + ACTIVE_SCROLLING_TIME;
    }
    isScrolling() {
        const scrollTime = this._scrollTime;
        if (scrollTime === 0) {
            return false;
        }
        if (scrollTime < Date.now()) {
            this._scrollTime = 0;
            return false;
        }
        return true;
    }
    getActiveNav() {
        const portal = this._appRoot._getPortal(MODAL);
        if (portal.length() > 0) {
            return findTopNav(portal);
        }
        return findTopNav(this._rootNav || null);
    }
    getRootNav() {
        return this._rootNav;
    }
    _setRootNav(nav) {
        this._rootNav = nav;
    }
    present(enteringView, opts, appPortal) {
        const 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);
    }
    goBack() {
        if (this._menuCtrl && this._menuCtrl.isOpen()) {
            return this._menuCtrl.close();
        }
        const navPromise = this.navPop();
        if (navPromise === null) {
            if (this._config.getBoolean('navExitApp', true)) {
                (void 0);
                this._platform.exitApp();
            }
        }
        return navPromise;
    }
    navPop() {
        if (!this._rootNav || !this.isEnabled()) {
            return Promise.resolve();
        }
        const 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 },] },
];
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;
}
const DEFAULT = 0;
const MODAL = 1;
const ACTIVE_SCROLLING_TIME = 100;
const CLICK_BLOCK_BUFFER_IN_MILLIS = 64;
//# sourceMappingURL=app.js.map