window.onload = function () {
  var files = [
    '{{bundlePath}}/vendors.bundle.js',
    '{{bundlePath}}/commons.bundle.js',
    '{{bundlePath}}/{{appId}}.bundle.js'
  ];

  var failure = function () {
    // make subsequent calls to failure() noop
    failure = function () {};

    var err = document.createElement('h1');
    err.style['color'] = 'white';
    err.style['font-family'] = 'monospace';
    err.style['text-align'] = 'center';
    err.style['background'] = '#F44336';
    err.style['padding'] = '25px';
    err.innerText = '{{i18n 'common.ui.welcomeError' '{"defaultMessage": "Kibana did not load properly. Check the server output for more information."}'}}';

    document.body.innerHTML = err.outerHTML;
  }

  function loadStyleSheet(path) {
    var dom = document.createElement('link');

    dom.addEventListener('error', failure);
    dom.setAttribute('rel', 'stylesheet');
    dom.setAttribute('href', path);
    document.head.appendChild(dom);
  }

  function createJavascriptElement(path) {
    var dom = document.createElement('script');

    dom.setAttribute('async', '');
    dom.addEventListener('error', failure);
    dom.setAttribute('src', file);
    dom.addEventListener('load', next);
    document.head.appendChild(dom);
  }

  {{#each styleSheetPaths}}
    loadStyleSheet('{{this}}');
  {{/each}}

  (function next() {
    var file = files.shift();
    if (!file) return;

    var dom = document.createElement('script');

    dom.setAttribute('async', '');
    dom.addEventListener('error', failure);
    dom.setAttribute('src', file);
    dom.addEventListener('load', next);
    document.head.appendChild(dom);
  }());
};
