'use strict';
(function(){
  angular.module('cla.controllers',[]);
  angular.module('cla.services',['ngResource']);
  angular.module('cla.services.operator', ['ngResource', 'cla.services']);
  angular.module('cla.services.provider', ['ngResource', 'cla.services']);
  angular.module('cla.filters',[]);
  angular.module('cla.directives',[]);
  angular.module('cla.states',[]);
  angular.module('cla.utils',[]);
  angular.module('cla.templates',[]);
  angular.module('cla.routes', ['cla.states']);

  var common_run;
  var common_config;

  common_run = ['$rootScope', '$state', '$stateParams', 'Timer', 'flash', 'cla.bus', 'History', '$uibModal', 'AssignProviderValidation', 'postal',
    function ($rootScope, $state, $stateParams, Timer, flash, bus, History, $uibModal, AssignProviderValidation, postal) {
      $rootScope.$state = $state;
      $rootScope.$stateParams = $stateParams;

      // handle state change errors
      $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){
        // generic state change error / redirect
        if (error.msg && !error.modal) {
          flash('error', error.msg);
        }
        // if content should appear in modal
        if (error.modal) {
          var opts = {
            templateUrl: 'invalid_modal.html',
            controller: 'InvalidCtrl',
            resolve: {
              tplVars: function () {
                return {
                  title: error.title,
                  message: error.msg,
                  errors: error.errors,
                  warnings: error.warnings
                };
              }
            }
          };
          var onConfirmSuccess = function (result) {
            if (result) {
              AssignProviderValidation.setWarned(true);
              $state.go(error.next, {caseref: error.case});
            } else {
              $state.go(error.goto, {caseref: error.case});
            }
          };
          var onDismiss = function () {
            $state.go(error.goto, {caseref: error.case});
          };

          $uibModal.open(opts).result.then(onConfirmSuccess, onDismiss);
        } else if (error.goto) {
          $state.go(error.goto, {caseref: error.case});
        }
      });
      $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        // log the previous state in the History
        History.previousState = fromState;
        History.previousState.params = fromParams;
      });

      Timer.install();
      bus.install();

      // set Piwik user id as logged in username
      postal.subscribe({
        channel: 'system',
        topic: 'user.identified',
        callback: function (user) {
          try {
            ga('set', '&uid', user.username);
          } catch (err) {
            console.warn('Google analytics is not installed', err);
          }
        }
      });
    }];

  common_config = ['$resourceProvider', 'cfpLoadingBarProvider', '$analyticsProvider',
    function($resourceProvider, cfpLoadingBarProvider, $analyticsProvider) {
    $resourceProvider.defaults.stripTrailingSlashes = false;
    cfpLoadingBarProvider.includeBar = false;
    $analyticsProvider.queryKeysBlacklist(['search']);
    }];


  //
  // Operator App
  //
  angular.module('cla.states.operator',['cla.states']);
  angular.module('cla.controllers.operator',[]);
  angular.module('cla.settings.operator', []).constant('AppSettings', {
    BASE_URL: '/call_centre/',
    timerEnabled: function() {
      return true;
    },
    callScriptEnabled: true,
    callMeBackEnabled: true,
    caseListRefreshDelay: 150000,  // in ms. -1 to disable it
    statesModule: 'cla.states.operator',
    appName: 'operator',
    tabWarningEnabled: false
  });

  angular.module('cla.operatorApp',
    [
      'cla.settings.operator',
      'cla.states.operator',
      'ngSanitize',
      'ngMessages',
      'angularMoment',
      'angular-blocks',
      'xeditable',
      'ui.router',
      'cla.constants',
      'cla.controllers',
      'cla.controllers.operator',
      'cla.services',
      'cla.services.operator',
      'cla.filters',
      'cla.directives',
      'cla.utils',
      'cla.templates',
      'cla.routes',
      'ui.bootstrap',
      'ui.select2',
      'hl.sticky',
      'angular-loading-bar',
      'angulartics',
      'angulartics.google.analytics',
      'cfp.hotkeys',
      'LocalStorageModule',
      'diff-match-patch',
      'angularUtils.directives.dirPagination',
      'ngTextTruncate',
      'ngIdle'
    ])
    .config(common_config)
    .run(common_run);


  //
  // Provider App
  //
  angular.module('cla.states.provider',['cla.states']);
  angular.module('cla.controllers.provider',[]);
  angular.module('cla.settings.provider', []).constant('AppSettings', {
    BASE_URL: '/provider/',
    timerEnabled: function() {
      return false;
    },
    callScriptEnabled: false,
    callMeBackEnabled: true,
    caseListRefreshDelay: 150000,  // in ms. -1 to disable it
    statesModule: 'cla.states.provider',
    appName: 'provider',
    tabWarningEnabled: false
  });

  angular.module('cla.providerApp',
    [
      'cla.settings.provider',
      'cla.states.provider',
      'ngSanitize',
      'ngMessages',
      'angularMoment',
      'angular-blocks',
      'xeditable',
      'ui.router',
      'cla.constants',
      'cla.controllers',
      'cla.controllers.provider',
      'cla.services',
      'cla.services.provider',
      'cla.filters',
      'cla.directives',
      'cla.utils',
      'cla.templates',
      'cla.routes',
      'ui.bootstrap',
      'ui.select2',
      'hl.sticky',
      'angular-loading-bar',
      'angulartics',
      'angulartics.google.analytics',
      'cfp.hotkeys',
      'LocalStorageModule',
      'diff-match-patch',
      'angularUtils.directives.dirPagination',
      'ngTextTruncate',
      'ngIdle'
    ])
    .config(common_config)
    .run(common_run);
})();

@@ -2398,16 +2398,312 @@
 History%0A
+        ga(function(tracker) %7B%0A          const location = tracker.get('location');%0A          if(location.includes('search')) %7B%0A            const urlSegments = location.split('?search');%0A            const newURL = urlSegments%5B0%5D;%0A            ga('set', 'location', newURL);%0A          %7D%0A        %7D);%0A
         
@@ -3380,16 +3380,18 @@
 ) %7B%0A    
+  
 $resourc
@@ -3435,24 +3435,26 @@
 es = false;%0A
+  
     cfpLoadi
@@ -3491,16 +3491,18 @@
 se;%0A    
+  
 $analyti
@@ -3544,18 +3544,16 @@
 rch'%5D);%0A
-  
   %7D%5D;%0A%0A%0A

Remove user data on refresh on the GA location property