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

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.run = 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/contrib";

  function prop(prop) {
    return function (o) {
      return o[prop];
    };
  }

  function findUsers() {
    var users = {};

    for (var _len = arguments.length, thingsToFind = Array(_len), _key = 0; _key < _len; _key++) {
      thingsToFind[_key] = arguments[_key];
    }

    thingsToFind.forEach(function (things) {
      things.forEach(function (thing) {
        if (thing.user) {
          users[thing.user.url] = true;
        }
      });
    });
    return Object.keys(users);
  }

  function join(things) {
    if (!things.length) {
      return "";
    }
    things = things.slice(0);
    var last = things.pop();
    var length = things.length;
    if (length === 0) {
      return last;
    }
    if (length === 1) {
      return things[0] + " and " + last;
    }
    return things.join(", ") + ", and " + last;
  }

  function toHTML(urls, editors, element) {
    return $.when.apply($, urls.map(function (url) {
      return (0, _github.fetch)(url);
    })).then(function () {
      for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }

      var names = args.map(function (user) {
        user = user[0];
        return user.name || user.login;
      }).filter(function (name) {
        return editors.indexOf(name) < 0;
      });
      names.sort(function (a, b) {
        return a.toLowerCase().localeCompare(b.toLowerCase());
      });
      $(element).html(join(names)).attr("id", null);
    });
  }

  var run = exports.run = function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(conf, doc, cb) {
      var $commenters, $contributors, elements;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              $commenters = doc.querySelector("#gh-commenters");
              $contributors = doc.querySelector("#gh-contributors");

              if (!(!$commenters && !$contributors)) {
                _context.next = 4;
                break;
              }

              return _context.abrupt("return", cb());

            case 4:
              if (conf.githubAPI) {
                _context.next = 11;
                break;
              }

              elements = [];

              if ($commenters) elements.push("#" + $commenters.id);
              if ($contributors) elements.push("#" + $contributors.id);
              (0, _pubsubhub.pub)("error", "Requested list of contributors and/or commenters from GitHub (" + elements.join(" and ") + ") but config.githubAPI is not set.");
              cb();
              return _context.abrupt("return");

            case 11:

              (0, _github.fetch)(conf.githubAPI).then(function (json) {
                return $.when((0, _github.fetchIndex)(json.issues_url), (0, _github.fetchIndex)(json.issue_comment_url), (0, _github.fetchIndex)(json.contributors_url));
              }).then(function (issues, comments, contributors) {
                var editors = respecConfig.editors.map(prop("name"));
                var commenters = findUsers(issues, comments);
                contributors = contributors.map(prop("url"));
                return $.when(toHTML(commenters, editors, $commenters), toHTML(contributors, editors, $contributors));
              }).then(cb, function (error) {
                (0, _pubsubhub.pub)("error", "Error loading contributors and/or commenters from  Error: " + error);
                cb();
              });

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

    return function run(_x, _x2, _x3) {
      return _ref.apply(this, arguments);
    };
  }();
});
//# sourceMappingURL=contrib.js.map