define(["exports", "core/post-process", "core/pre-process", "core/pubsubhub", "core/default-root-attr", "core/include-config", "core/override-configuration", "core/remove-respec", "core/respec-ready", "deps/regenerator"], function (exports, _postProcess, _preProcess, _pubsubhub) {
  "use strict";

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.runAll = exports.name = undefined;

  function _asyncToGenerator(fn) {
    return function () {
      var gen = fn.apply(this, arguments);
      return new Promise(function (resolve, reject) {
        function step(key, arg) {
          try {
            var info = gen[key](arg);
            var value = info.value;
          } catch (error) {
            reject(error);
            return;
          }

          if (info.done) {
            resolve(value);
          } else {
            return Promise.resolve(value).then(function (value) {
              step("next", value);
            }, function (err) {
              step("throw", err);
            });
          }
        }

        return step("next");
      });
    };
  }

  var name = exports.name = "core/base-runner";
  var canMeasure = performance.mark && performance.measure;

  function toRunnable(plug) {
    var _this = this;

    var name = plug.name || "";
    if (!name) {
      console.warn("Plugin lacks name:", plug);
    }
    return function (config) {
      return new Promise(function () {
        var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(resolve, reject) {
          var timerId;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  timerId = setTimeout(function () {
                    var msg = "Plugin " + name + " took too long.";
                    console.error(msg, plug);
                    reject(new Error(msg));
                  }, 5000);
                  // Modern plugins are async or normal functions, take one argument (conf)

                  if (canMeasure) {
                    performance.mark(name + "-start");
                  }
                  _context.prev = 2;

                  if (!(plug.run.length === 1)) {
                    _context.next = 9;
                    break;
                  }

                  _context.next = 6;
                  return plug.run(config);

                case 6:
                  resolve();
                  _context.next = 10;
                  break;

                case 9:
                  plug.run(config, document, resolve);

                case 10:
                  _context.next = 15;
                  break;

                case 12:
                  _context.prev = 12;
                  _context.t0 = _context["catch"](2);

                  reject(_context.t0);

                case 15:
                  _context.prev = 15;

                  clearTimeout(timerId);
                  return _context.finish(15);

                case 18:
                  if (canMeasure) {
                    performance.mark(name + "-end");
                    performance.measure(name, name + "-start", name + "-end");
                  }

                case 19:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, _this, [[2, 12, 15, 18]]);
        }));

        return function (_x, _x2) {
          return _ref.apply(this, arguments);
        };
      }());
    };
  }

  var runAll = exports.runAll = function () {
    var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(plugs) {
      var runnables, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, task;

      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              (0, _pubsubhub.pub)("start-all", respecConfig);
              if (canMeasure) {
                performance.mark(name + "-start");
              }
              _context2.next = 4;
              return _preProcess.done;

            case 4:
              runnables = plugs.filter(function (plug) {
                return plug.run;
              }).map(toRunnable);
              _iteratorNormalCompletion = true;
              _didIteratorError = false;
              _iteratorError = undefined;
              _context2.prev = 8;
              _iterator = runnables[Symbol.iterator]();

            case 10:
              if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
                _context2.next = 23;
                break;
              }

              task = _step.value;
              _context2.prev = 12;
              _context2.next = 15;
              return task(respecConfig);

            case 15:
              _context2.next = 20;
              break;

            case 17:
              _context2.prev = 17;
              _context2.t0 = _context2["catch"](12);

              console.error(_context2.t0);

            case 20:
              _iteratorNormalCompletion = true;
              _context2.next = 10;
              break;

            case 23:
              _context2.next = 29;
              break;

            case 25:
              _context2.prev = 25;
              _context2.t1 = _context2["catch"](8);
              _didIteratorError = true;
              _iteratorError = _context2.t1;

            case 29:
              _context2.prev = 29;
              _context2.prev = 30;

              if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
              }

            case 32:
              _context2.prev = 32;

              if (!_didIteratorError) {
                _context2.next = 35;
                break;
              }

              throw _iteratorError;

            case 35:
              return _context2.finish(32);

            case 36:
              return _context2.finish(29);

            case 37:
              (0, _pubsubhub.pub)("plugins-done", respecConfig);
              _context2.next = 40;
              return _postProcess.done;

            case 40:
              (0, _pubsubhub.pub)("end-all", respecConfig);
              if (canMeasure) {
                performance.mark(name + "-end");
                performance.measure(name, name + "-start", name + "-end");
              }

            case 42:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, this, [[8, 25, 29, 37], [12, 17], [30,, 32, 36]]);
    }));

    return function runAll(_x3) {
      return _ref2.apply(this, arguments);
    };
  }();
});
//# sourceMappingURL=base-runner.js.map