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

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

  var _l10n2 = _interopRequireDefault(_l10n);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: obj
    };
  }

  var _slicedToArray = function () {
    function sliceIterator(arr, i) {
      var _arr = [];
      var _n = true;
      var _d = false;
      var _e = undefined;

      try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);

          if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;
        _e = err;
      } finally {
        try {
          if (!_n && _i["return"]) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }

      return _arr;
    }

    return function (arr, i) {
      if (Array.isArray(arr)) {
        return arr;
      } else if (Symbol.iterator in Object(arr)) {
        return sliceIterator(arr, i);
      } else {
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
      }
    };
  }();

  var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
    return typeof obj;
  } : function (obj) {
    return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  };

  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/github";

  function findNext(header) {
    // Finds the next URL of paginated resources which
    // is available in the Link header. Link headers look like this:
    // Link: <url1>; rel="next", <url2>; rel="foo"; bar="baz"
    // More info here: https://developer.github.com/v3/#link-header
    var m = (header || "").match(/<([^>]+)>\s*;\s*rel="next"/);
    return m && m[1] || null;
  }

  function fetch(url, options) {
    if (options) {
      options.url = url;
      url = options;
    }
    return $.ajax(url);
  }

  function fetchAll(url, options) {
    var output = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];

    var request = fetch(url, options);
    return request.then(function (resp) {
      output.push.apply(output, resp);
      var next = findNext(request.getResponseHeader("Link"));
      return next ? fetchAll(next, options, output) : output;
    });
  }

  function fetchIndex(url, options) {
    // converts URLs of the form:
    // https://api.github.com/repos/user/repo/comments{/number}
    // into:
    // https://api.github.com/repos/user/repo/comments
    // which is what you need if you want to get the index.
    return fetchAll(url.replace(/\{[^}]+\}/, ""), options);
  }

  var run = exports.run = function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(conf) {
      var ghURL, _ghURL$pathname$split, _ghURL$pathname$split2, org, repo, branch, newProps, commitsHref, otherLink;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(!conf.hasOwnProperty("github") || !conf.github)) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return");

            case 2:
              if (!(_typeof(conf.github) === "object" && !conf.github.hasOwnProperty("repoURL"))) {
                _context.next = 5;
                break;
              }

              (0, _pubsubhub.pub)("error", "`respecConf.github` missing property `repoURL`. See https://github.com/w3c/respec/wiki/github");
              return _context.abrupt("return");

            case 5:
              ghURL = void 0;
              _context.prev = 6;

              ghURL = new URL(conf.github.repoURL || conf.github);
              _context.next = 14;
              break;

            case 10:
              _context.prev = 10;
              _context.t0 = _context["catch"](6);

              (0, _pubsubhub.pub)("error", "`respecConf.github` is not a valid URL? (" + ghURL + ")");
              return _context.abrupt("return");

            case 14:
              if (!(ghURL.origin !== "https://github.com")) {
                _context.next = 17;
                break;
              }

              (0, _pubsubhub.pub)("error", "`respecConf.github` must be HTTPS and pointing to GitHub. (" + ghURL + ")");
              return _context.abrupt("return");

            case 17:
              _ghURL$pathname$split = ghURL.pathname.split("/").filter(function (item) {
                return item;
              }), _ghURL$pathname$split2 = _slicedToArray(_ghURL$pathname$split, 2), org = _ghURL$pathname$split2[0], repo = _ghURL$pathname$split2[1];

              if (!org || !repo) {
                (0, _pubsubhub.pub)("error", "`respecConf.github` URL needs a path with, for example, w3c/my-spec");
              }
              branch = conf.github.branch || "gh-pages";
              newProps = {
                shortName: repo,
                edDraftURI: "https://" + org.toLowerCase() + ".github.io/" + repo + "/",
                githubAPI: "https://api.github.com/repos/" + org + "/" + repo,
                issueBase: "" + ghURL.href + (ghURL.pathname.endsWith("/") ? "" : "/") + "issues/"
              };
              commitsHref = "" + ghURL.href + (ghURL.pathname.endsWith("/") ? "" : "/") + "commits/" + branch;
              otherLink = {
                key: conf.l10n.participate,
                data: [{
                  value: "GitHub " + org + "/" + repo,
                  href: ghURL
                }, {
                  value: conf.l10n.file_a_bug,
                  href: newProps.issueBase
                }, {
                  value: conf.l10n.commit_history,
                  href: commitsHref
                }]
              };
              // Write new properties, ignoring existing ones

              Object.getOwnPropertyNames(newProps).filter(function (key) {
                return !conf.hasOwnProperty(key);
              }).map(function (key) {
                return { key: key, value: newProps[key] };
              }).reduce(function (conf, _ref2) {
                var key = _ref2.key,
                    value = _ref2.value;

                conf[key] = value;
                return conf;
              }, conf);
              if (!conf.hasOwnProperty("otherLinks")) {
                conf.otherLinks = [];
              }
              conf.otherLinks.unshift(otherLink);

            case 26:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this, [[6, 10]]);
    }));

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