'use strict';

(() => {
  const loadScripts = (urls, succesCB, failCB) => {
    let count = urls.length;
    let errored = false;

    if (urls.length == 0) return succesCB();

    urls.forEach(function(url) {
      var script = document.createElement('script');
      script.onload = function() {
        if (errored) return;
        if (!--count) succesCB();
      };
      script.onerror = function() {
        if (errored) return;
        failCB();
        errored = true;
      };
      script.src = url;
      document.head.insertBefore(script, document.head.firstChild);
    });
  };


  // The base path of the polyfills
  const scriptSrc = document.querySelector('script[src*="polyfills/polyfills.js"]').src;
  let basePath = scriptSrc.match(/^(.*)polyfills\/polyfills\.js$/i);
  if (basePath) {
    basePath = basePath[1];
  }

  const polyfillsNeeded = [];
  let waitForWebcomponents = false;

  if (!('Promise' in window)) {
    polyfillsNeeded.push(basePath+'polyfills/promise.js');
  }

  if(!('URL') in window) {
    polyfillsNeeded.push(basePath+'polyfills/url.js');
  }

  if (!('fetch' in window)) {
    polyfillsNeeded.push(basePath+'polyfills/fetch/fetch.js');
  }

  if (!('registerElement' in document)) {
    polyfillsNeeded.push(basePath+ 'polyfills/webcomponentsjs/CustomElements.min.js');
    waitForWebcomponents = true;
  }

  // Safari bug work around, safari sees HTMLElement
  // as an object not a function.
  if (typeof HTMLElement !== 'function') {
    const _HTMLElement = function() {};
    _HTMLElement.prototype = HTMLElement.prototype;
    HTMLElement = _HTMLElement;
  }

  const polyFillsLoadedEvent = new CustomEvent('polyFillsLoaded', {
    bubbles: true
  });

  // Load the polyfills
  loadScripts(polyfillsNeeded, () => {
    if (waitForWebcomponents) {
      window.addEventListener('WebComponentsReady', () => {
        requestAnimationFrame(() => {
          document.dispatchEvent(polyFillsLoadedEvent);
        });
      })
    } else {
      requestAnimationFrame(() => {
        document.dispatchEvent(polyFillsLoadedEvent);
      });
    }
  }, () => {
    throw new Error('Failed to load required polyfills');
  });
})();

@@ -13,23 +13,50 @@
 ;%0A%0A(
-() =%3E %7B%0A  const
+function () %7B%0A  var loadScripts = function
  loa
@@ -63,19 +63,16 @@
 dScripts
- = 
 (urls, s
@@ -91,21 +91,18 @@
 lCB)
- =%3E
  %7B%0A    
-let
+var
  cou
@@ -123,19 +123,19 @@
 th;%0A    
-let
+var
  errored
@@ -216,16 +216,17 @@
 function
+ 
 (url) %7B%0A
@@ -300,32 +300,33 @@
 nload = function
+ 
 () %7B%0A        if 
@@ -356,16 +356,17 @@
    if (!
+ 
 --count)
@@ -414,24 +414,25 @@
 r = function
+ 
 () %7B%0A       
@@ -610,17 +610,16 @@
 ;%0A  %7D;%0A%0A
-%0A
   // The
@@ -648,21 +648,19 @@
 fills%0A  
-const
+var
  scriptS
@@ -735,19 +735,19 @@
 .src;%0A  
-let
+var
  basePat
@@ -851,29 +851,27 @@
 %5B1%5D;%0A  %7D%0A%0A  
-const
+var
  polyfillsNe
@@ -887,11 +887,11 @@
 ;%0A  
-let
+var
  wai
@@ -975,33 +975,35 @@
 ed.push(basePath
-+
+ + 
 'polyfills/promi
@@ -1024,17 +1024,16 @@
   if
+ 
 (!
-(
 'URL'
-)
  in 
@@ -1067,33 +1067,35 @@
 ed.push(basePath
-+
+ + 
 'polyfills/url.j
@@ -1167,17 +1167,19 @@
 basePath
-+
+ + 
 'polyfil
@@ -1279,16 +1279,17 @@
 basePath
+ 
 + 'polyf
@@ -1506,21 +1506,19 @@
 ) %7B%0A    
-const
+var
  _HTMLEl
@@ -1533,16 +1533,29 @@
 function
+ _HTMLElement
 () %7B%7D;%0A 
@@ -1648,13 +1648,11 @@
 %0A%0A  
-const
+var
  pol
@@ -1788,21 +1788,27 @@
 Needed, 
-() =%3E
+function ()
  %7B%0A    i
@@ -1886,21 +1886,27 @@
 Ready', 
-() =%3E
+function ()
  %7B%0A     
@@ -1922,37 +1922,43 @@
 tAnimationFrame(
-() =%3E
+function ()
  %7B%0A          doc
@@ -2020,16 +2020,17 @@
       %7D)
+;
 %0A    %7D e
@@ -2063,21 +2063,27 @@
 onFrame(
-() =%3E
+function ()
  %7B%0A     
@@ -2156,13 +2156,19 @@
  %7D, 
-() =%3E
+function ()
  %7B%0A 

converted polyfills to es5