import { Component, ComponentFactoryResolver, ElementRef, Inject, OpaqueToken, Renderer, ViewChild, ViewContainerRef } from '@angular/core';
import { App } from './app';
import { Config } from '../../config/config';
import { Ion } from '../ion';
import { OverlayPortal } from '../nav/overlay-portal';
import { Platform } from '../../platform/platform';
import { nativeTimeout } from '../../util/dom';
export const AppRootToken = new OpaqueToken('USERROOT');
export class IonicApp extends Ion {
    constructor(_userCmp, _cfr, elementRef, renderer, config, _platform, app) {
        super(config, elementRef, renderer);
        this._userCmp = _userCmp;
        this._cfr = _cfr;
        this._platform = _platform;
        app._appRoot = this;
        this._stopScrollPlugin = window['IonicStopScroll'];
    }
    ngOnInit() {
        const factory = this._cfr.resolveComponentFactory(this._userCmp);
        const componentRef = this._viewport.createComponent(factory);
        this._renderer.setElementClass(componentRef.location.nativeElement, 'app-root', true);
        componentRef.changeDetectorRef.detectChanges();
        this.setElementClass(this._config.get('mode'), true);
        const versions = this._platform.versions();
        this._platform.platforms().forEach(platformName => {
            let platformClass = 'platform-' + platformName;
            this.setElementClass(platformClass, true);
            let platformVersion = versions[platformName];
            if (platformVersion) {
                platformClass += platformVersion.major;
                this.setElementClass(platformClass, true);
                this.setElementClass(platformClass + '_' + platformVersion.minor, true);
            }
        });
        if (this._config.getBoolean('hoverCSS', true)) {
            this.setElementClass('enable-hover', true);
        }
        this._platform.prepareReady();
    }
    _getPortal(portal) {
        if (portal === 2) {
            return this._loadingPortal;
        }
        if (portal === 3) {
            return this._toastPortal;
        }
        if (portal === 1) {
            return this._modalPortal;
        }
        return this._overlayPortal;
    }
    _getActivePortal() {
        const defaultPortal = this._overlayPortal;
        const modalPortal = this._modalPortal;
        const hasModal = modalPortal.length() > 0;
        const hasDefault = defaultPortal.length() > 0;
        if (!hasModal && !hasDefault) {
            return null;
        }
        else if (hasModal && hasDefault) {
            var defaultIndex = defaultPortal.getActive().getZIndex();
            var modalIndex = modalPortal.getActive().getZIndex();
            if (defaultIndex > modalIndex) {
                return defaultPortal;
            }
            else {
                (void 0);
                return modalPortal;
            }
        }
        if (hasModal) {
            return modalPortal;
        }
        else if (hasDefault) {
            return defaultPortal;
        }
    }
    _disableScroll(shouldDisableScroll) {
        if (shouldDisableScroll) {
            this.stopScroll().then(() => {
                this._rafId = nativeTimeout(() => {
                    (void 0);
                    this.setElementClass('disable-scroll', true);
                }, 16 * 2);
            });
        }
        else {
            let plugin = this._stopScrollPlugin;
            if (plugin && plugin.cancel) {
                plugin.cancel();
            }
            clearTimeout(this._rafId);
            (void 0);
            this.setElementClass('disable-scroll', false);
        }
    }
    stopScroll() {
        if (this._stopScrollPlugin) {
            return new Promise((resolve, reject) => {
                this._stopScrollPlugin.stop(() => resolve(true));
            });
        }
        else {
            return Promise.resolve(false);
        }
    }
}
IonicApp.decorators = [
    { type: Component, args: [{
                selector: 'ion-app',
                template: '<div #viewport app-viewport></div>' +
                    '<div #modalPortal overlay-portal></div>' +
                    '<div #overlayPortal overlay-portal></div>' +
                    '<div #loadingPortal class="loading-portal" overlay-portal></div>' +
                    '<div #toastPortal class="toast-portal" [overlay-portal]="10000"></div>' +
                    '<div class="click-block"></div>'
            },] },
];
IonicApp.ctorParameters = [
    { type: undefined, decorators: [{ type: Inject, args: [AppRootToken,] },] },
    { type: ComponentFactoryResolver, },
    { type: ElementRef, },
    { type: Renderer, },
    { type: Config, },
    { type: Platform, },
    { type: App, },
];
IonicApp.propDecorators = {
    '_viewport': [{ type: ViewChild, args: ['viewport', { read: ViewContainerRef },] },],
    '_modalPortal': [{ type: ViewChild, args: ['modalPortal', { read: OverlayPortal },] },],
    '_overlayPortal': [{ type: ViewChild, args: ['overlayPortal', { read: OverlayPortal },] },],
    '_loadingPortal': [{ type: ViewChild, args: ['loadingPortal', { read: OverlayPortal },] },],
    '_toastPortal': [{ type: ViewChild, args: ['toastPortal', { read: OverlayPortal },] },],
};
;
//# sourceMappingURL=app-root.js.map