/* globals PROVIDER_GLOBAL */
/*eslint no-unused-vars: 0*/

window.PROVIDER_GLOBAL = window.PROVIDER_GLOBAL || {};
PROVIDER_GLOBAL.cmd = PROVIDER_GLOBAL.cmd || [];
(function() {
  var _availability = '';
  var _initializing = false;
  var _initialized = false;
  var _cmdQ = [];
  var drainQ = function(reasonToDrain) {
    var i;
    var len = _cmdQ.length;
    var stopEarly = false;
    // console.log(reasonToDrain);
    for (i = 0; i < len; i++) {
      _cmdQ[i]();
      if (_initializing) {
        stopEarly = true;
        break;
      }
    }
    if (stopEarly) {
      _cmdQ = _cmdQ.slice(i + 1);
    } else {
      _cmdQ = [];
    }
  };
  PROVIDER_GLOBAL.init = function(queryParams) {
    if (_initialized) {
      console.log('duplicate init call for PROVIDER_GLOBAL');
      return;
    }
    _initializing = true;
    var initURL = '/simulated-provider/PROVIDER_GLOBAL/refresh?' + queryParams;
    // TODO make this async ready
    window.document.write('<script src="' + initURL + '"></script>');
  };
  PROVIDER_GLOBAL.resume = function() {
    _initializing = false;
    _initialized = true;
    drainQ('resume drain');
  };
  PROVIDER_GLOBAL.setAvailable = function(availability) {
    _availability = availability;
  };
  PROVIDER_GLOBAL.getAvailable = function() {
    return _availability;
  };
  _cmdQ = PROVIDER_GLOBAL.cmd;
  console.log();
  drainQ('initial drain');
  // allow pushing on more cmd callbacks
  PROVIDER_GLOBAL.cmd = {
    push: function() {
      _cmdQ = Array.prototype.slice.call(arguments);
      drainQ('cmd.push drain');
    }
  };
}());

@@ -398,16 +398,35 @@
 ole.log(
+'PROVIDER_GLOBAL', 
 reasonTo
@@ -433,16 +433,16 @@
 Drain);%0A
-
     for 
@@ -939,99 +939,859 @@
 ODO 
-make this async ready%0A    window.document.write('%3Cscript src=%22' + initURL + '%22%3E%3C/script%3E');
+consider window.document or just raw document instead%0A    var doc = window.document;%0A    // Note: currentScript is not generally available across browsers%0A    var cur = doc.currentScript;%0A    if ((PROVIDER_GLOBAL.useAsync %7C%7C 'complete' === doc.readyState %7C%7C 'loaded' === doc.readyState %7C%7C cur && cur.async)) %7B%0A      // load async%0A      var script = doc.createElement('script');%0A      script.src = initURL;%0A      script.async = true;%0A      (doc.head %7C%7C doc.body %7C%7C doc.documentElement).appendChild(script);%0A      PROVIDER_GLOBAL._init_started = true;%0A    %7D else %7B%0A      // load sync%0A      var providerId = 'PROVIDER_GLOBAL-init-' + Math.random();%0A      try %7B%0A        doc.write('%3Cscript id=%22' + providerId + '%22 src=%22' + initURL + '%22%3E%3C/script%3E');%0A      %7D catch (e) %7B %7D%0A      doc.getElementById(providerId) && (PROVIDER_GLOBAL._init_started = true);%0A    %7D
 %0A  %7D
@@ -2091,16 +2091,16 @@
 y;%0A  %7D;%0A
+
   _cmdQ 
@@ -2126,25 +2126,8 @@
 md;%0A
-  console.log();%0A
   dr

make simulated tag provider minimally sync / async aware