define(["exports", "core/pubsubhub", "deps/regenerator"], function (exports, _pubsubhub) {
  "use strict";

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

  var _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  function _toConsumableArray(arr) {
    if (Array.isArray(arr)) {
      for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
        arr2[i] = arr[i];
      }

      return arr2;
    } else {
      return Array.from(arr);
    }
  }

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

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var name = exports.name = "core/linter";
  var privates = new WeakMap();

  var Linter = function () {
    function Linter() {
      _classCallCheck(this, Linter);

      privates.set(this, {
        rules: new Set()
      });
    }

    _createClass(Linter, [{
      key: "register",
      value: function register() {
        for (var _len = arguments.length, newRules = Array(_len), _key = 0; _key < _len; _key++) {
          newRules[_key] = arguments[_key];
        }

        newRules.reduce(function (rules, newRule) {
          return rules.add(newRule);
        }, this.rules);
      }
    }, {
      key: "lint",
      value: function () {
        var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(conf) {
          var doc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.document;
          var promisesToLint;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  promisesToLint = [].concat(_toConsumableArray(privates.get(this).rules)).map(function (rule) {
                    return toLinterWarning(rule.lint(conf, doc));
                  });
                  _context.next = 3;
                  return promisesToLint;

                case 3:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, this);
        }));

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

        return lint;
      }()
    }, {
      key: "rules",
      get: function get() {
        return privates.get(this).rules;
      }
    }]);

    return Linter;
  }();

  var linter = new Linter();
  exports.default = linter;


  var baseResult = {
    name: "unknown",
    description: "",
    occurrences: 0,
    howToFix: "",
    offendingElements: [], // DOM Nodes
    help: "" // where to get help
  };

  var toLinterWarning = function () {
    var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(promiseToLint) {
      var _this = this;

      var results;
      return regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _context4.next = 2;
              return promiseToLint;

            case 2:
              results = _context4.sent;

              results.map(function () {
                var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(resultPromise) {
                  var _console;

                  var result, output, description, help, howToFix, name, occurrences, offendingElements, msg;
                  return regeneratorRuntime.wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          _context2.next = 2;
                          return resultPromise;

                        case 2:
                          result = _context2.sent;
                          output = _extends({}, baseResult, result);
                          description = output.description, help = output.help, howToFix = output.howToFix, name = output.name, occurrences = output.occurrences, offendingElements = output.offendingElements;
                          msg = description + " " + howToFix + " " + help + " (\"" + name + "\" x " + occurrences + ")";

                          offendingElements.forEach(function (elem) {
                            elem.classList.add("respec-offending-element");
                          });
                          (_console = console).warn.apply(_console, ["Linter (" + name + "):", description].concat(_toConsumableArray(offendingElements)));
                          return _context2.abrupt("return", msg);

                        case 9:
                        case "end":
                          return _context2.stop();
                      }
                    }
                  }, _callee2, _this);
                }));

                return function (_x4) {
                  return _ref3.apply(this, arguments);
                };
              }()).forEach(function () {
                var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(msgPromise) {
                  return regeneratorRuntime.wrap(function _callee3$(_context3) {
                    while (1) {
                      switch (_context3.prev = _context3.next) {
                        case 0:
                          _context3.t0 = _pubsubhub.pub;
                          _context3.next = 3;
                          return msgPromise;

                        case 3:
                          _context3.t1 = _context3.sent;
                          (0, _context3.t0)("warn", _context3.t1);

                        case 5:
                        case "end":
                          return _context3.stop();
                      }
                    }
                  }, _callee3, _this);
                }));

                return function (_x5) {
                  return _ref4.apply(this, arguments);
                };
              }());

            case 4:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4, this);
    }));

    return function toLinterWarning(_x3) {
      return _ref2.apply(this, arguments);
    };
  }();

  var run = exports.run = function () {
    var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(conf, doc, cb) {
      return regeneratorRuntime.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              cb(); // return early, continue processing other things

              if (!(conf.lint === false)) {
                _context5.next = 3;
                break;
              }

              return _context5.abrupt("return");

            case 3:
              _context5.next = 5;
              return document.respecReady;

            case 5:
              _context5.prev = 5;
              _context5.next = 8;
              return linter.lint(conf, doc);

            case 8:
              _context5.next = 13;
              break;

            case 10:
              _context5.prev = 10;
              _context5.t0 = _context5["catch"](5);

              console.error("Error ocurred while running the linter", _context5.t0);

            case 13:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5, this, [[5, 10]]);
    }));

    return function run(_x6, _x7, _x8) {
      return _ref5.apply(this, arguments);
    };
  }();
});
//# sourceMappingURL=linter.js.map