"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var src_1 = require('../../../../src');
function run() {
    describe('App', function () {
        describe('navPop', function () {
            it('should select the previous tab', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                nav.registerChildNav(tabs);
                tabs.select(tab1);
                tabs.select(tab2);
                expect(tabs.selectHistory).toEqual([tab1.id, tab2.id]);
                spyOn(platform, 'exitApp');
                spyOn(tabs, 'select');
                spyOn(tab1, 'pop');
                spyOn(tab2, 'pop');
                spyOn(portal, 'pop');
                app.navPop();
                expect(tabs.select).toHaveBeenCalledWith(tab1);
                expect(tab1.pop).not.toHaveBeenCalled();
                expect(tab2.pop).not.toHaveBeenCalled();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop from the active tab, when tabs is nested is the root nav', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                var tab3 = mockTab(tabs);
                nav.registerChildNav(tabs);
                tab2.setSelected(true);
                spyOn(platform, 'exitApp');
                spyOn(tab2, 'pop');
                spyOn(portal, 'pop');
                var view1 = new src_1.ViewController();
                var view2 = new src_1.ViewController();
                tab2._views = [view1, view2];
                app.navPop();
                expect(tab2.pop).toHaveBeenCalled();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop from the active tab, when tabs is the root', function () {
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                var tab3 = mockTab(tabs);
                app.setRootNav(tabs);
                tab2.setSelected(true);
                spyOn(platform, 'exitApp');
                spyOn(tab2, 'pop');
                var view1 = new src_1.ViewController();
                var view2 = new src_1.ViewController();
                tab2._views = [view1, view2];
                app.navPop();
                expect(tab2.pop).toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop the root nav when nested nav has less than 2 views', function () {
                var rootNav = mockNav();
                var nestedNav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                rootNav.registerChildNav(nestedNav);
                nestedNav.parent = rootNav;
                app.setRootNav(rootNav);
                spyOn(platform, 'exitApp');
                spyOn(rootNav, 'pop');
                spyOn(nestedNav, 'pop');
                spyOn(portal, 'pop');
                var rootView1 = new src_1.ViewController();
                var rootView2 = new src_1.ViewController();
                rootNav._views = [rootView1, rootView2];
                var nestedView1 = new src_1.ViewController();
                nestedNav._views = [nestedView1];
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(rootNav.pop).toHaveBeenCalled();
                expect(nestedNav.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop a view from the nested nav that has more than 1 view', function () {
                var rootNav = mockNav();
                var nestedNav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(rootNav);
                rootNav.registerChildNav(nestedNav);
                spyOn(platform, 'exitApp');
                spyOn(rootNav, 'pop');
                spyOn(nestedNav, 'pop');
                spyOn(portal, 'pop');
                var rootView1 = new src_1.ViewController();
                var rootView2 = new src_1.ViewController();
                rootNav._views = [rootView1, rootView2];
                var nestedView1 = new src_1.ViewController();
                var nestedView2 = new src_1.ViewController();
                nestedNav._views = [nestedView1, nestedView2];
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(rootNav.pop).not.toHaveBeenCalled();
                expect(nestedNav.pop).toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop the overlay in the portal of the root nav', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                spyOn(platform, 'exitApp');
                spyOn(nav, 'pop');
                spyOn(portal, 'pop');
                var view1 = new src_1.ViewController();
                var view2 = new src_1.ViewController();
                nav._views = [view1, view2];
                var overlay = new src_1.ViewController();
                portal._views = [overlay];
                app.navPop();
                expect(portal.pop).toHaveBeenCalled();
                expect(nav.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should pop the second view in the root nav', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                spyOn(platform, 'exitApp');
                spyOn(nav, 'pop');
                spyOn(portal, 'pop');
                var view1 = new src_1.ViewController();
                var view2 = new src_1.ViewController();
                nav._views = [view1, view2];
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(nav.pop).toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should exit app when only one view in the root nav', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                spyOn(platform, 'exitApp');
                spyOn(nav, 'pop');
                spyOn(portal, 'pop');
                var view1 = new src_1.ViewController();
                nav._views = [view1];
                expect(app.getActiveNav()).toBe(nav);
                expect(nav.first()).toBe(view1);
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(nav.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).toHaveBeenCalled();
            });
            it('should not exit app when only one view in the root nav, but navExitApp config set', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                spyOn(platform, 'exitApp');
                spyOn(nav, 'pop');
                spyOn(portal, 'pop');
                config.set('navExitApp', false);
                var view1 = new src_1.ViewController();
                nav._views = [view1];
                expect(app.getActiveNav()).toBe(nav);
                expect(nav.first()).toBe(view1);
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(nav.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should not go back if app is not enabled', function () {
                var nav = mockNav();
                var portal = mockNav();
                app.setPortal(portal);
                app.setRootNav(nav);
                spyOn(platform, 'exitApp');
                spyOn(nav, 'pop');
                spyOn(portal, 'pop');
                var view1 = new src_1.ViewController();
                nav._views = [view1];
                app.setEnabled(false, 10000);
                app.navPop();
                expect(portal.pop).not.toHaveBeenCalled();
                expect(nav.pop).not.toHaveBeenCalled();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
            it('should not go back if there is no root nav', function () {
                spyOn(platform, 'exitApp');
                app.navPop();
                expect(platform.exitApp).not.toHaveBeenCalled();
            });
        });
        describe('getActiveNav', function () {
            it('should get active NavController when using tabs with nested nav', function () {
                var nav = mockNav();
                app.setRootNav(nav);
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                nav.registerChildNav(tabs);
                tab2.setSelected(true);
                var nav2 = mockNav();
                var nav3 = mockNav();
                var nav4 = mockNav();
                tab1.registerChildNav(nav4);
                tab2.registerChildNav(nav2);
                tab2.registerChildNav(nav3);
                expect(app.getActiveNav()).toBe(nav3);
            });
            it('should get active NavController when using tabs, nested in a root nav', function () {
                var nav = mockNav();
                app.setRootNav(nav);
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                var tab3 = mockTab(tabs);
                nav.registerChildNav(tabs);
                tab2.setSelected(true);
                expect(app.getActiveNav()).toBe(tab2);
                tab2.setSelected(false);
                tab3.setSelected(true);
                expect(app.getActiveNav()).toBe(tab3);
            });
            it('should get active tab NavController when using tabs, and tabs is the root', function () {
                var tabs = mockTabs();
                var tab1 = mockTab(tabs);
                var tab2 = mockTab(tabs);
                var tab3 = mockTab(tabs);
                app.setRootNav(tabs);
                tab2.setSelected(true);
                expect(app.getActiveNav()).toBe(tab2);
                tab2.setSelected(false);
                tab3.setSelected(true);
                expect(app.getActiveNav()).toBe(tab3);
            });
            it('should get active NavController when nested 3 deep', function () {
                var nav1 = mockNav();
                var nav2 = mockNav();
                var nav3 = mockNav();
                app.setRootNav(nav1);
                nav1.registerChildNav(nav2);
                nav2.registerChildNav(nav3);
                expect(app.getActiveNav()).toBe(nav3);
            });
            it('should get active NavController when nested 2 deep', function () {
                var nav1 = mockNav();
                var nav2 = mockNav();
                app.setRootNav(nav1);
                nav1.registerChildNav(nav2);
                expect(app.getActiveNav()).toBe(nav2);
            });
            it('should get active NavController when only one nav controller', function () {
                var nav = mockNav();
                app.setRootNav(nav);
                expect(app.getActiveNav()).toBe(nav);
            });
            it('should set/get the root nav controller', function () {
                var nav = mockNav();
                app.setRootNav(nav);
                expect(app.getRootNav()).toBe(nav);
            });
            it('should not get an active NavController if there is not root set', function () {
                expect(app.getActiveNav()).toBeNull();
                expect(app.getRootNav()).toBeNull();
            });
        });
        describe('setEnabled', function () {
            it('should disable click block when app is enabled', function () {
                // arrange
                var mockClickBlock = {
                    activate: function () { }
                };
                spyOn(mockClickBlock, 'activate');
                app.clickBlock = mockClickBlock;
                // act
                app.setEnabled(true);
                // assert
                expect(mockClickBlock.activate).toHaveBeenCalledWith(false, 0);
            });
            it('should disable click block when app is disabled but duration of less than 32 passed', function () {
                // arrange
                var mockClickBlock = {
                    activate: function () { }
                };
                spyOn(mockClickBlock, 'activate');
                app.clickBlock = mockClickBlock;
                // act
                app.setEnabled(false, 20);
                // assert
                expect(mockClickBlock.activate).toHaveBeenCalledWith(false, 0);
            });
            it('should enable click block when false is passed with duration', function () {
                // arrange
                var mockClickBlock = {
                    activate: function () { }
                };
                spyOn(mockClickBlock, 'activate');
                app.clickBlock = mockClickBlock;
                // act
                app.setEnabled(false, 200);
                // assert
                expect(mockClickBlock.activate).toHaveBeenCalledWith(true, 200 + 64);
            });
            it('should enable click block when false is passed w/o duration', function () {
                // arrange
                var mockClickBlock = {
                    activate: function () { }
                };
                spyOn(mockClickBlock, 'activate');
                app.clickBlock = mockClickBlock;
                // act
                app.setEnabled(false);
                // assert
                // 700 is the default
                expect(mockClickBlock.activate).toHaveBeenCalledWith(true, 700 + 64);
            });
        });
        var app;
        var config;
        var platform;
        var _cd;
        function mockNav() {
            return new src_1.Nav(null, null, null, config, null, null, null, null, null);
        }
        function mockTabs() {
            return new src_1.Tabs(null, null, null, config, null, null, null);
        }
        function mockTab(parentTabs) {
            var tab = new src_1.Tab(parentTabs, app, config, null, null, null, null, null, _cd);
            parentTabs.add(tab);
            tab.root = SomePage;
            tab.load = function (opts, cb) {
                cb();
            };
            return tab;
        }
        var SomePage = (function () {
            function SomePage() {
            }
            SomePage = __decorate([
                core_1.Component({}), 
                __metadata('design:paramtypes', [])
            ], SomePage);
            return SomePage;
        }());
        beforeEach(function () {
            config = new src_1.Config();
            platform = new src_1.Platform();
            app = new src_1.App(config, platform);
            _cd = {
                reattach: function () { },
                detach: function () { }
            };
        });
    });
}
exports.run = run;