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

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

/**
 * Add `options` and `version` to `babel` global.
 */

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

/**
 * Add `transform` api to `babel` global.
 */

transform.transform = transform;

/**
 * Tranform and execute script, adding in inline sourcemaps.
 */

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

/**
 * Load scripts via xhr, and `transform` when complete (optional).
 */

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");

  /**
   * When successfully loaded, transform (optional), and call `callback`.
   */

  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);
};

/**
 * Load and transform all scripts of `types`.
 *
 * @example
 * <script type="module"></script>
 */

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

  /**
   * Transform and execute script. Ensures correct load order.
   */

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

  /**
   * Load, transform, and execute all scripts.
   */

  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];
    }
  };

  // Collect scripts with Babel `types`.

  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();
};

/**
 * Register load event to transform and execute scripts.
 */

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

@@ -26,438 +26,75 @@
 */%0A%0A
-require(%22./node%22);%0Avar transform = module.exports = require(%22../transformation%22);%0A%0A/**%0A * Add %60options%60 and %60version%60 to %60babel%60 global.%0A */%0A%0Atransform.options = require(%22../transformation/file/options%22);%0Atransform.version = require(%22../../package%22).version;%0A%0A/**%0A * Add %60transform%60 api to %60babel%60 global.%0A */%0A%0Atransform.transform = transform;%0A%0A/**%0A * Tranform and execute script, adding in inline sourcemaps.%0A */%0A%0Atransform.run =
+import %7B transform %7D from %22./node%22;%0Aexport * from %22./node%22;%0A%0Aexport
  fun
@@ -99,16 +99,19 @@
 unction 
+run
 (code, o
@@ -126,38 +126,8 @@
 ) %7B%0A
-  opts.sourceMaps = %22inline%22;%0A
   re
@@ -180,113 +180,30 @@
 );%0A%7D
-;
 %0A%0A
-/**%0A * Load scripts via xhr, and %60transform%60 when complete (optional).%0A */%0A%0Atransform.load = function 
+export function load
 (url
@@ -490,95 +490,8 @@
 );%0A%0A
-  /**%0A   * When successfully loaded, transform (optional), and call %60callback%60.%0A   */%0A%0A
   xh
@@ -702,39 +702,12 @@
 ld) 
-transform.run.apply(transform, 
+run(
 para
@@ -843,142 +843,29 @@
 );%0A%7D
-;
 %0A%0A
-/**%0A * Load and transform all scripts of %60types%60.%0A *%0A * @example%0A * %3Cscript type=%22module%22%3E%3C/script%3E%0A */%0A%0Avar runScripts = function 
+function runScripts
 () %7B
@@ -1062,19 +1062,8 @@
 /%0A%0A 
- var exec =
  fun
@@ -1060,32 +1060,36 @@
  */%0A%0A  function 
+exec
 () %7B%0A    var par
@@ -1153,39 +1153,12 @@
     
-transform.run.apply(transform, 
+run(
 para
@@ -1195,25 +1195,24 @@
 );%0A    %7D%0A  %7D
-;
 %0A%0A  /**%0A   *
@@ -1266,18 +1266,8 @@
 /%0A%0A 
- var run =
  fun
@@ -1272,16 +1272,19 @@
 unction 
+run
 (script,
@@ -1341,18 +1341,8 @@
     
-transform.
 load
@@ -1536,25 +1536,24 @@
 %5D;%0A    %7D%0A  %7D
-;
 %0A%0A  // Colle
@@ -1860,17 +1860,16 @@
 xec();%0A%7D
-;
 %0A%0A/**%0A *

clean up browser API