define(["exports", "deps/webidl2"], function (exports, _webidl) {
  "use strict";

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

  function _defineProperty(obj, key, value) {
    if (key in obj) {
      Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
      });
    } else {
      obj[key] = value;
    }

    return obj;
  }

  function findParentByName(localName) {
    return function recurse(elem) {
      var _ref;

      if (!elem.parentElement) {
        return null;
      }
      while (elem.parentElement.localName !== localName) {
        return recurse(elem.parentElement);
      }
      return _ref = {}, _defineProperty(_ref, localName, elem.parentElement), _defineProperty(_ref, "elem", elem), _ref;
    };
  }

  function findTopLevelIdl(parsedIdl) {
    return parsedIdl.find(function (_ref2) {
      var type = _ref2.type;
      return ["dictionary", "enum", "interface"].includes(type);
    });
  }

  var findSectionParent = findParentByName("section");

  function createHeading(name, type) {
    var h2 = document.createElement("h2");
    h2.innerHTML = "<dfn>" + name + "</dfn> " + type;
    return h2;
  }

  var run = exports.run = function () {
    var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(_ref4, doc, cb) {
      var enableExperimentalFeatures = _ref4.enableExperimentalFeatures;
      var sections;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (enableExperimentalFeatures) {
                _context.next = 2;
                break;
              }

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

            case 2:
              sections = Array.from(doc.querySelectorAll("section pre.idl")).map(findSectionParent).map(function (_ref5) {
                var section = _ref5.section,
                    pre = _ref5.elem;
                return { section: section, pre: pre, idl: (0, _webidl.parse)(pre.textContent) };
              }).filter(function (_ref6) {
                var idl = _ref6.idl;
                return idl.some(function (_ref7) {
                  var type = _ref7.type;
                  return ["dictionary", "enum", "interface"].includes(type);
                });
              }).forEach(function (_ref8) {
                var section = _ref8.section,
                    pre = _ref8.pre,
                    idl = _ref8.idl;

                var hasHeading = section.querySelector(":scope>h2,:scope>h3,:scope>h4,:scope>h5,:scope>h6");

                var _findTopLevelIdl = findTopLevelIdl(idl),
                    name = _findTopLevelIdl.name,
                    type = _findTopLevelIdl.type;

                if (hasHeading === null) {
                  var heading = createHeading(name, type);
                  section.insertBefore(heading, section.firstChild);
                }
                if (!section.dataset.dfnFor) {
                  section.dataset.dfnFor = name;
                }
                if (!section.dataset.linkFor) {
                  section.dataset.linkFor = name;
                }
              });

              cb();

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

    return function run(_x, _x2, _x3) {
      return _ref3.apply(this, arguments);
    };
  }();
});
//# sourceMappingURL=webidl-sections.js.map