define(["exports", "core/biblio", "core/data-cite", "core/utils"], function (exports, _biblio, _dataCite, _utils) {
  "use strict";

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

  function _toArray(arr) {
    return Array.isArray(arr) ? arr : Array.from(arr);
  }

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

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

  var name = exports.name = "core/cite-summary";

  function toListItems(dataSource) {
    return function () {
      for (var _len = arguments.length, terms = Array(_len), _key = 0; _key < _len; _key++) {
        terms[_key] = arguments[_key];
      }

      return terms.map(function (term) {
        return "<li><dfn data-cite=\"\">" + term + "</dfn></li>";
      });
    };
  }

  function toFragment(text) {
    var elem = document.createElement("temp");
    elem.innerHTML = text;
    return Array.from(elem.childNodes).reduce(function (frag, node) {
      frag.appendChild(node);
      return frag;
    }, document.createDocumentFragment());
  }

  var run = exports.run = function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(conf, doc, cb) {
      var citeSummary, heading, defs, lookupResults, content;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              citeSummary = doc.querySelector("section#cite-summary");

              if (citeSummary) {
                _context.next = 3;
                break;
              }

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

            case 3:

              if (!(0, _utils.hasImmidiateHeadings)(citeSummary)) {
                heading = doc.createElement("h2");

                heading.innerHTML = "Dependencies";
                if (citeSummary.firstChild) {
                  citeSummary.insertBefore(heading, citeSummary.firstChild);
                } else {
                  citeSummary.appendChild(heading);
                }
              }

              defs = [].concat(_toConsumableArray(doc.querySelectorAll("a[data-cite]"))).map(function (elem) {
                return {
                  text: elem.textContent.replace(/\s+/g, " ").trim(),
                  xref: elem.dataset.cite
                };
              }).reduce(function (map, _ref2) {
                var xref = _ref2.xref,
                    text = _ref2.text;

                if (map.has(xref)) {
                  map.get(xref).add(text);
                } else {
                  map.set(xref, new Set([text]));
                }
                return map;
              }, new Map());
              lookupResults = Array.from(defs.entries()
              // don't redefine things that already exist
              ).filter(function (_ref3) {
                var _ref4 = _slicedToArray(_ref3, 1),
                    xref = _ref4[0];

                return !doc.querySelector("dfn[data-cite=\"" + xref + "\"]");
              }).map(function (_ref5) {
                var _ref6 = _slicedToArray(_ref5, 2),
                    xref = _ref6[0],
                    linkedTerms = _ref6[1];

                var xrefObj = (0, _dataCite.toXrefObject)(xref);
                return { xref: xrefObj, linkedTerms: linkedTerms };
              });
              content = lookupResults.map(function (result) {
                var _result$xref = result.xref,
                    key = _result$xref.key,
                    isNormative = _result$xref.isNormative;

                var citeKey = isNormative ? "!" + key : key;

                var _Array$from = Array.from(result.linkedTerms),
                    _Array$from2 = _toArray(_Array$from),
                    firstTerm = _Array$from2[0],
                    linkedTerms = _Array$from2.slice(1);

                var html = "\n        <p>The following are defined in [[" + citeKey + "]]:</p>\n        <ul>\n          <li><dfn data-lt=\"" + linkedTerms.join("|") + "\">" + firstTeFastcore19rm + "</dfn></li>\n        </ul>\n      ";
                return toFragment(html.trim());
              }).reduce(function (frag, node) {
                frag.appendChild(node);
                return frag;
              }, doc.createDocumentFragment());

              if (content.hasChildNodes()) {
                citeSummary.appendChild(content);
              }
              cb();

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

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