import { EventEmitter, Output } from '@angular/core';
import { isPresent, assign } from '../util/util';
import { NavParams } from './nav-params';
export var ViewController = (function () {
    function ViewController(component, data, rootCssClass) {
        if (rootCssClass === void 0) { rootCssClass = DEFAULT_CSS_CLASS; }
        this.component = component;
        this._isHidden = false;
        this.isOverlay = false;
        this._emitter = new EventEmitter();
        this.data = (data instanceof NavParams ? data.data : (isPresent(data) ? data : {}));
        this._cssClass = rootCssClass;
        this.willEnter = new EventEmitter();
        this.didEnter = new EventEmitter();
        this.willLeave = new EventEmitter();
        this.didLeave = new EventEmitter();
        this.willUnload = new EventEmitter();
    }
    ViewController.prototype.init = function (componentRef) {
        this._cmp = componentRef;
        this.instance = this.instance || componentRef.instance;
        this._detached = false;
    };
    ViewController.prototype._setNav = function (navCtrl) {
        this._nav = navCtrl;
    };
    ViewController.prototype._setInstance = function (instance) {
        this.instance = instance;
    };
    ViewController.prototype.subscribe = function (generatorOrNext) {
        return this._emitter.subscribe(generatorOrNext);
    };
    ViewController.prototype.emit = function (data) {
        this._emitter.emit(data);
    };
    ViewController.prototype.onDismiss = function (callback) {
        console.warn('onDismiss(..) has been deprecated. Please use onDidDismiss(..) instead');
        this.onDidDismiss(callback);
    };
    ViewController.prototype.onDidDismiss = function (callback) {
        this._onDidDismiss = callback;
    };
    ViewController.prototype.onWillDismiss = function (callback) {
        this._onWillDismiss = callback;
    };
    ViewController.prototype.dismiss = function (data, role, navOptions) {
        var _this = this;
        if (navOptions === void 0) { navOptions = {}; }
        if (!this._nav) {
            return Promise.resolve(false);
        }
        var options = assign({}, this._leavingOpts, navOptions);
        this._onWillDismiss && this._onWillDismiss(data, role);
        return this._nav.remove(this._nav.indexOf(this), 1, options).then(function () {
            _this._onDidDismiss && _this._onDidDismiss(data, role);
            _this._onDidDismiss = null;
            return data;
        });
    };
    ViewController.prototype.getNav = function () {
        return this._nav;
    };
    ViewController.prototype.getTransitionName = function (direction) {
        return this._nav && this._nav.config.get('pageTransition');
    };
    ViewController.prototype.getNavParams = function () {
        return new NavParams(this.data);
    };
    ViewController.prototype.setLeavingOpts = function (opts) {
        this._leavingOpts = opts;
    };
    ViewController.prototype.enableBack = function () {
        if (this._nav) {
            var previousItem = this._nav.getPrevious(this);
            return !!(previousItem);
        }
        return false;
    };
    Object.defineProperty(ViewController.prototype, "name", {
        get: function () {
            return this.component ? this.component.name : '';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(ViewController.prototype, "index", {
        get: function () {
            return (this._nav ? this._nav.indexOf(this) : -1);
        },
        enumerable: true,
        configurable: true
    });
    ViewController.prototype.isFirst = function () {
        return (this._nav ? this._nav.first() === this : false);
    };
    ViewController.prototype.isLast = function () {
        return (this._nav ? this._nav.last() === this : false);
    };
    ViewController.prototype._domShow = function (shouldShow, renderer) {
        if (this._cmp) {
            if (shouldShow === this._isHidden) {
                this._isHidden = !shouldShow;
                var value = (shouldShow ? null : '');
                renderer.setElementAttribute(this.pageRef().nativeElement, 'hidden', value);
            }
        }
    };
    ViewController.prototype._setZIndex = function (zIndex, renderer) {
        if (zIndex !== this._zIndex) {
            this._zIndex = zIndex;
            var pageRef = this.pageRef();
            if (pageRef) {
                renderer.setElementStyle(pageRef.nativeElement, 'z-index', zIndex);
            }
        }
    };
    ViewController.prototype.pageRef = function () {
        return this._cmp && this._cmp.location;
    };
    ViewController.prototype._setContent = function (directive) {
        this._cntDir = directive;
    };
    ViewController.prototype.getContent = function () {
        return this._cntDir;
    };
    ViewController.prototype._setContentRef = function (elementRef) {
        this._cntRef = elementRef;
    };
    ViewController.prototype.contentRef = function () {
        return this._cntRef;
    };
    ViewController.prototype._setHeader = function (directive) {
        this._hdrDir = directive;
    };
    ViewController.prototype.getHeader = function () {
        return this._hdrDir;
    };
    ViewController.prototype._setFooter = function (directive) {
        this._ftrDir = directive;
    };
    ViewController.prototype.getFooter = function () {
        return this._ftrDir;
    };
    ViewController.prototype._setNavbar = function (directive) {
        this._nb = directive;
    };
    ViewController.prototype.getNavbar = function () {
        return this._nb;
    };
    ViewController.prototype.hasNavbar = function () {
        return !!this._nb;
    };
    ViewController.prototype.setBackButtonText = function (val) {
        this._nb && this._nb.setBackButtonText(val);
    };
    ViewController.prototype.showBackButton = function (shouldShow) {
        if (this._nb) {
            this._nb.hideBackButton = !shouldShow;
        }
    };
    ViewController.prototype._willLoad = function () {
        ctrlFn(this, 'WillLoad');
    };
    ViewController.prototype._didLoad = function () {
        if (this.instance && this.instance.ionViewLoaded) {
            try {
                console.warn('ionViewLoaded() has been deprecated. Please rename to ionViewDidLoad()');
                this.instance.ionViewLoaded();
            }
            catch (e) {
                console.error(this.name + ' iionViewLoaded: ' + e.message);
            }
        }
        ctrlFn(this, 'DidLoad');
    };
    ViewController.prototype._willEnter = function () {
        if (this._detached && this._cmp) {
            this._cmp.changeDetectorRef.reattach();
            this._detached = false;
        }
        this.willEnter.emit(null);
        ctrlFn(this, 'WillEnter');
    };
    ViewController.prototype._didEnter = function () {
        this._nb && this._nb.didEnter();
        this.didEnter.emit(null);
        ctrlFn(this, 'DidEnter');
    };
    ViewController.prototype._willLeave = function () {
        this.willLeave.emit(null);
        ctrlFn(this, 'WillLeave');
    };
    ViewController.prototype._didLeave = function () {
        this.didLeave.emit(null);
        ctrlFn(this, 'DidLeave');
        if (!this._detached && this._cmp) {
            this._cmp.changeDetectorRef.detach();
            this._detached = true;
        }
    };
    ViewController.prototype._willUnload = function () {
        this.willUnload.emit(null);
        ctrlFn(this, 'WillUnload');
        if (this.instance && this.instance.ionViewDidUnload) {
            console.warn('ionViewDidUnload() has been deprecated. Please use ionViewWillUnload() instead');
            try {
                this.instance.ionViewDidUnload();
            }
            catch (e) {
                console.error(this.name + ' ionViewDidUnload: ' + e.message);
            }
        }
    };
    ViewController.prototype._destroy = function (renderer) {
        if (this._cmp) {
            if (renderer) {
                renderer.setElementAttribute(this._cmp.location.nativeElement, 'class', null);
                renderer.setElementAttribute(this._cmp.location.nativeElement, 'style', null);
            }
            this._cmp.destroy();
        }
        if (this._nav) {
            var index = this._nav.indexOf(this);
            if (index > -1) {
                this._nav._views.splice(index, 1);
            }
        }
        this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._hdrDir = this._ftrDir = this._nb = this._onWillDismiss = null;
    };
    ViewController.prototype._lifecycleTest = function (lifecycle) {
        var instance = this.instance;
        var methodName = 'ionViewCan' + lifecycle;
        if (instance && instance[methodName]) {
            try {
                return instance[methodName]();
            }
            catch (e) {
                console.error(this.name + " ionViewCan" + lifecycle + " error: " + e);
                return false;
            }
        }
        return true;
    };
    ViewController.propDecorators = {
        '_emitter': [{ type: Output },],
    };
    return ViewController;
}());
function ctrlFn(viewCtrl, fnName) {
    if (viewCtrl.instance) {
        if (viewCtrl.instance['ionView' + fnName]) {
            try {
                viewCtrl.instance['ionView' + fnName]();
            }
            catch (e) {
                console.error(viewCtrl.name + ' ionView' + fnName + ': ' + e.message);
            }
        }
    }
}
export function isViewController(viewCtrl) {
    return !!(viewCtrl && viewCtrl._didLoad && viewCtrl._willUnload);
}
var DEFAULT_CSS_CLASS = 'ion-page';
