var uniflash;
(function (uniflash) {
    var session;
    (function (session) {
        'use strict';
        angular.module('uniflash').run(['$rootScope', '$state', 'ufSession',
            function ($rootScope, $state, ufSession) {
                var confirmed = false;
                $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState) {
                    // if we are leaving the main page to go back to the welcome page
                    if (toState.name === "welcome" && _.startsWith(fromState.name, "main") && !confirmed) {
                        event.preventDefault();
                        ufSession.confirmLeave()
                            .then(function () {
                            // state change will trigger another stateChangeStart
                            // there for we need to set a flag saying we have already confirmed
                            confirmed = true;
                            $state.go(toState.name).finally(function () {
                                confirmed = false;
                            });
                        });
                    }
                });
            }
        ]);
    })(session = uniflash.session || (uniflash.session = {}));
})(uniflash || (uniflash = {}));
