// This polyfill is needed to support PhantomJS which we use to generate PNGs from embeds.
import 'core-js/fn/typed/array-buffer';

import 'material-design-iconic-font/dist/css/material-design-iconic-font.css';
import 'font-awesome/css/font-awesome.css';
import 'ui-select/dist/select.css';
import 'angular-toastr/dist/angular-toastr.css';
import 'angular-resizable/src/angular-resizable.css';
import 'angular-gridster/dist/angular-gridster.css';
import 'pace-progress/themes/blue/pace-theme-minimal.css';

import 'pace-progress';
import debug from 'debug';
import angular from 'angular';
import ngSanitize from 'angular-sanitize';
import ngRoute from 'angular-route';
import ngResource from 'angular-resource';
import uiBootstrap from 'angular-ui-bootstrap';
import uiSelect from 'ui-select';
import ngMessages from 'angular-messages';
import toastr from 'angular-toastr';
import ngUpload from 'angular-base64-upload';
import vsRepeat from 'angular-vs-repeat';
import 'angular-moment';
import 'brace';
import 'angular-ui-ace';
import 'angular-resizable';
import ngGridster from 'angular-gridster';
import { each } from 'underscore';

import './sortable';

import './assets/css/superflat_redash.css';
import './assets/css/redash.css';
import './assets/css/main.scss';

import * as pages from './pages';
import * as components from './components';
import * as filters from './filters';
import * as services from './services';
import registerDirectives from './directives';
import registerVisualizations from './visualizations';
import markdownFilter from './filters/markdown';
import dateTimeFilter from './filters/datetime';

const logger = debug('redash');

const requirements = [
  ngRoute, ngResource, ngSanitize, uiBootstrap, ngMessages, uiSelect, 'angularMoment', toastr, 'ui.ace',
  ngUpload, 'angularResizable', vsRepeat, 'ui.sortable', ngGridster.name,
];

const ngModule = angular.module('app', requirements);

function registerComponents() {
  each(components, (register) => {
    register(ngModule);
  });
}

function registerServices() {
  each(services, (register) => {
    register(ngModule);
  });
}

function registerPages() {
  each(pages, (registerPage) => {
    const routes = registerPage(ngModule);

    ngModule.config(($routeProvider) => {
      each(routes, (route, path) => {
        logger('Route: ', path);
        // This is a workaround, to make sure app-header and footer are loaded only
        // for the authenticated routes.
        // We should look into switching to ui-router, that has built in support for
        // such things.
        route.template = `<app-header></app-header><route-status></route-status>${route.template}<footer></footer>`;
        route.authenticated = true;
        $routeProvider.when(path, route);
      });
    });
  });
}

function registerFilters() {
  each(filters, (filter, name) => {
    ngModule.filter(name, () => filter);
  });
}

registerDirectives(ngModule);
registerServices();
registerFilters();
markdownFilter(ngModule);
dateTimeFilter(ngModule);
registerComponents();
registerPages();
registerVisualizations(ngModule);

ngModule.config((
  $routeProvider, $locationProvider, $compileProvider,
  uiSelectConfig, toastrConfig,
) => {
  $compileProvider.debugInfoEnabled(false);
  $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|http|data):/);
  $locationProvider.html5Mode(true);
  uiSelectConfig.theme = 'bootstrap';

  Object.assign(toastrConfig, {
    positionClass: 'toast-bottom-right',
    timeOut: 2000,
  });
});

// Update ui-select's template to use Font-Awesome instead of glyphicon.
ngModule.run(($templateCache, OfflineListener) => { // eslint-disable-line no-unused-vars
  const templateName = 'bootstrap/match.tpl.html';
  let template = $templateCache.get(templateName);
  template = template.replace('glyphicon glyphicon-remove', 'fa fa-remove');
  $templateCache.put(templateName, template);
});

export default ngModule;

@@ -1647,20 +1647,25 @@
 ('redash
+:init
 ');%0A
-
 %0Aconst r
@@ -1690,16 +1690,18 @@
 ngRoute,
+%0A 
  ngResou
@@ -1704,16 +1704,18 @@
 esource,
+%0A 
  ngSanit
@@ -1718,16 +1718,18 @@
 anitize,
+%0A 
  uiBoots
@@ -1733,16 +1733,18 @@
 otstrap,
+%0A 
  ngMessa
@@ -1747,16 +1747,18 @@
 essages,
+%0A 
  uiSelec
@@ -1759,16 +1759,18 @@
 iSelect,
+%0A 
  'angula
@@ -1782,16 +1782,18 @@
 nt',
+%0A 
  toastr,
  'ui
@@ -1788,16 +1788,18 @@
  toastr,
+%0A 
  'ui.ace
@@ -1812,16 +1812,18 @@
 gUpload,
+%0A 
  'angula
@@ -1834,16 +1834,18 @@
 izable',
+%0A 
  vsRepea
@@ -1846,16 +1846,18 @@
 sRepeat,
+%0A 
  'ui.sor
@@ -1863,16 +1863,18 @@
 rtable',
+%0A 
  ngGrids
@@ -2344,14 +2344,28 @@
 r('R
+egistering r
 oute: 
+%25s
 ', p
@@ -3157,27 +3157,8 @@
 ig((
-%0A  $routeProvider, 
 $loc
@@ -3189,18 +3189,16 @@
 rovider,
-%0A 
  uiSelec
@@ -3218,18 +3218,16 @@
 trConfig
-,%0A
 ) =%3E %7B%0A 
@@ -3595,16 +3595,59 @@
 phicon.%0A
+// eslint-disable-next-line no-unused-vars%0A
 ngModule
@@ -3693,46 +3693,8 @@
 =%3E %7B
- // eslint-disable-line no-unused-vars
 %0A  c

Add logging to route registration.