/* eslint no-new-func: 0 */

require("./node");
var transform = module.exports = require("../transformation");

/**
 * [Please add a description.]
 */

transform.options = require("../transformation/file/options");
transform.version = require("../../../package").version;

transform.transform = transform;

/**
 * [Please add a description.]
 */

transform.run = function (code, opts = {}) {
  opts.sourceMaps = "inline";
  return new Function(transform(code, opts).code)();
};

/**
 * [Please add a description.]
 */

transform.load = function (url, callback, opts = {}, hold) {
  opts.filename = opts.filename || url;

  var xhr = global.ActiveXObject ? new global.ActiveXObject("Microsoft.XMLHTTP") : new global.XMLHttpRequest();
  xhr.open("GET", url, true);
  if ("overrideMimeType" in xhr) xhr.overrideMimeType("text/plain");

  /**
   * [Please add a description.]
   */

  xhr.onreadystatechange = function () {
    if (xhr.readyState !== 4) return;

    var status = xhr.status;
    if (status === 0 || status === 200) {
      var param = [xhr.responseText, opts];
      if (!hold) transform.run.apply(transform, param);
      if (callback) callback(param);
    } else {
      throw new Error(`Could not load ${url}`);
    }
  };

  xhr.send(null);
};

/**
 * [Please add a description.]
 */

var runScripts = function () {
  var scripts = [];
  var types   = ["text/ecmascript-6", "text/6to5", "text/babel", "module"];
  var index   = 0;

  /**
   * [Please add a description.]
   */

  var exec = function () {
    var param = scripts[index];
    if (param instanceof Array) {
      transform.run.apply(transform, param);
      index++;
      exec();
    }
  };

  /**
   * [Please add a description.]
   */

  var run = function (script, i) {
    var opts = {};

    if (script.src) {
      transform.load(script.src, function (param) {
        scripts[i] = param;
        exec();
      }, opts, true);
    } else {
      opts.filename = "embedded";
      scripts[i] = [script.innerHTML, opts];
    }
  };

  var _scripts = global.document .getElementsByTagName("script");

  for (var i = 0; i < _scripts.length; ++i) {
    var _script = _scripts[i];
    if (types.indexOf(_script.type) >= 0) scripts.push(_script);
  }

  for (i in scripts) {
    run(scripts[i], i);
  }

  exec();
};

/**
 * [Please add a description.]
 */

if (global.addEventListener) {
  global.addEventListener("DOMContentLoaded", runScripts, false);
} else if (global.attachEvent) {
  global.attachEvent("onload", runScripts);
}

@@ -116,35 +116,54 @@
 %0A * 
-%5BPlease add a description.%5D
+Add %60options%60 and %60version%60 to %60babel%60 global.
 %0A */
@@ -285,16 +285,67 @@
 rsion;%0A%0A
+/**%0A * Add %60transform%60 api to %60babel%60 global.%0A */%0A%0A
 transfor
@@ -381,35 +381,65 @@
 %0A * 
-%5BPlease add a description.%5D
+Tranform and execute script, adding in inline sourcemaps.
 %0A */
@@ -583,35 +583,71 @@
 %0A * 
-%5BPlease add a descri
+Load scripts via xhr, and %60transform%60 when complete (o
 ption
+al)
 .
-%5D
 %0A */
@@ -977,35 +977,76 @@
   * 
-%5BPlease add a description.%5D
+When successfully loaded, transform (optional), and call %60callback%60.
 %0A   
@@ -1443,35 +1443,100 @@
 %0A * 
-%5BPlease add a de
+Load and transform all scripts of %60types%60.%0A *%0A * @example%0A * %3Cscript type=%22module%22%3E%3C/
 script
-ion.%5D
+%3E
 %0A */
@@ -1699,35 +1699,65 @@
   * 
-%5BPlease add a description.%5D
+Transform and execute script. Ensures correct load order.
 %0A   
@@ -1954,35 +1954,49 @@
   * 
-%5BPlease add a de
+Load, transform, and execute all 
 script
-ion.%5D
+s.
 %0A   
@@ -2298,16 +2298,58 @@
 %7D%0A  %7D;%0A%0A
+  // Collect scripts with Babel %60types%60.%0A%0A
   var _s
@@ -2372,17 +2372,16 @@
 document
- 
 .getElem
@@ -2630,35 +2630,61 @@
 %0A * 
-%5BPlease add a de
+Register load event to transform and execute 
 script
-ion.%5D
+s.
 %0A */

Add descriptions to api/browser