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

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

  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 toLookupRequest = function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(elem) {
      var originalKey, _toCiteDetails, key, frag, entry, msg, href, a;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              originalKey = elem.dataset.cite;
              _toCiteDetails = toCiteDetails(elem), key = _toCiteDetails.key, frag = _toCiteDetails.frag;
              _context.next = 4;
              return (0, _biblio.resolveRef)(key);

            case 4:
              entry = _context.sent;

              cleanElement(elem);

              if (entry) {
                _context.next = 12;
                break;
              }

              msg = "Couldn't find a match for 'data-cite=" + originalKey + "'.";

              console.warn(msg, elem);
              msg += " Please check developer console for offending element.";
              (0, _pubsubhub.pub)("warn", msg);
              return _context.abrupt("return");

            case 12:
              href = entry.href;

              if (frag) {
                href += frag;
              }
              _context.t0 = elem.localName;
              _context.next = _context.t0 === "a" ? 17 : _context.t0 === "dfn" ? 19 : 24;
              break;

            case 17:
              elem.href = href;
              return _context.abrupt("break", 24);

            case 19:
              a = elem.ownerDocument.createElement("a");

              a.href = href;
              while (elem.firstChild) {
                a.appendChild(elem.firstChild);
              }
              elem.appendChild(a, elem);
              return _context.abrupt("break", 24);

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

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

  function cleanElement(elem) {
    ["data-cite", "data-cite-frag"].filter(function (attrName) {
      return elem.hasAttribute(attrName);
    }).forEach(function (attrName) {
      return elem.removeAttribute(attrName);
    });
  }

  function toCiteDetails(_ref2) {
    var dataset = _ref2.dataset;
    var key = dataset.cite,
        frag = dataset.citeFrag;

    var isNormative = key.startsWith("!");
    var fragPosition = key.search("#");
    if (fragPosition !== -1) {
      frag = !frag ? key.substr(fragPosition) : frag;
      key = key.substring(0, fragPosition);
    }
    if (isNormative) {
      key = key.substr(1);
    }
    if (frag && !frag.startsWith("#")) {
      frag = "#" + frag;
    }
    return { key: key, isNormative: isNormative, frag: frag };
  }

  function run(conf, doc, cb) {
    Array.from(doc.querySelectorAll(["dfn[data-cite], a[data-cite]"])).filter(function (el) {
      return el.dataset.cite;
    }).map(toCiteDetails).reduce(function (conf, _ref3) {
      var isNormative = _ref3.isNormative,
          key = _ref3.key;

      if (isNormative) {
        conf.normativeReferences.add(key);
      } else {
        conf.informativeReferences.add(key);
      }
      return conf;
    }, conf);
    cb();
  }

  var linkInlineCitations = exports.linkInlineCitations = function () {
    var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(doc) {
      var citedSpecs, lookupRequests;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              citedSpecs = doc.querySelectorAll("dfn[data-cite], a[data-cite]");
              lookupRequests = Array.from(citedSpecs).map(toLookupRequest);
              _context2.next = 4;
              return Promise.all(lookupRequests);

            case 4:
              return _context2.abrupt("return", _context2.sent);

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, this);
    }));

    return function linkInlineCitations(_x2) {
      return _ref4.apply(this, arguments);
    };
  }();
});
//# sourceMappingURL=data-cite.js.map