/* eslint-disable */

(function() {
  'use strict';

  var rootPath = getScriptPath();

  if (isES6()) {
    window.WebComponents = window.WebComponents || {};
    window.WebComponents.root = rootPath + 'wc/';

    var onloadCallback = function() {
      window.WebComponents.waitFor(function() {
        addScript(rootPath + 'zui-bundle.js', { module: true });
      });
    };

    addScript(rootPath + 'wc/webcomponents-loader.js', { onloadCallback: onloadCallback, module: true });
  } else {
    // es5 bundle already includes all the polyfills
    addScript(rootPath + 'zui-bundle.es5.js');
  }

  function getScriptPath() {
    var src = '/';
    if (document.currentScript) {
      src = document.currentScript.src;
    } else {
      var scriptName = '@zywave/zui-bundle';
      var thisScript = document.querySelector('script[src*="' + scriptName + '"]');
      if (thisScript) {
        src = thisScript.src;
      }
    }
    var dir = src.substring(0, src.lastIndexOf('/'));
    return dir + '/';
  }

  // from https://stackoverflow.com/questions/29046635/javascript-es6-cross-browser-detection
  function isES6() {
    'use strict';

    if (typeof Symbol == 'undefined') {
      return false;
    }
    try {
      eval('class Foo {}');
      eval('var bar = (x) => x+1');
    } catch (e) {
      return false;
    }

    return true;
  }

  function addScript(src, opts) {
    var onloadCallback = opts && opts.onloadCallback;
    var module = opts && opts.module;
    var tag = document.createElement('script');
    tag.src = src;
    if (onloadCallback) {
      tag.addEventListener('load', onloadCallback);
    }
    if (module) {
      tag.type = 'module';
    }
    document.head.appendChild(tag);
  }
})();
