define(["exports", "deps/text!core/css/github.css", "core/pubsubhub", "core/worker", "deps/regenerator"], function (exports, _github, _pubsubhub, _worker) {
  "use strict";

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

  var _github2 = _interopRequireDefault(_github);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      default: 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/highlight";

  // Opportunistically insert the style into the head to reduce FOUC.
  var codeStyle = document.createElement("style");
  codeStyle.textContent = _github2.default;
  document.head.appendChild(codeStyle);
  var idCounter = 0;
  function getLanguageHint(classList) {
    return Array.from(classList).filter(function (item) {
      return item !== "highlight" && item !== "nolinks";
    }).map(function (item) {
      return item.toLowerCase();
    });
  }

  var run = exports.run = function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(conf) {
      var highlightables, promisesToHighlight;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!conf.noHighlightCSS) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return");

            case 2:
              highlightables = Array.from(document.querySelectorAll("pre:not(.idl):not(.nohighlight), code.highlight"));

              if (highlightables.length) {
                _context.next = 5;
                break;
              }

              return _context.abrupt("return");

            case 5:
              promisesToHighlight = highlightables.map(function (element) {
                return new Promise(function (resolve) {
                  if (element.textContent.trim() === "") {
                    return resolve(element); // no work to do
                  }
                  var done = function done() {
                    element.setAttribute("aria-busy", "false");
                    resolve(element);
                  };
                  // We always resolve, even if we couldn't actually highlight
                  var timeoutId = setTimeout(function () {
                    console.error("Timed-out waiting for highlight:", element);
                    done();
                  }, 1000);
                  var msg = {
                    action: "highlight",
                    code: element.textContent,
                    id: "highlight:" + idCounter++,
                    languages: getLanguageHint(element.classList)
                  };
                  element.setAttribute("aria-busy", "true");
                  element.setAttribute("aria-live", "polite");
                  _worker.worker.postMessage(msg);
                  _worker.worker.addEventListener("message", function listener(ev) {
                    var _ev$data = ev.data,
                        id = _ev$data.id,
                        code = _ev$data.code,
                        language = _ev$data.language,
                        value = _ev$data.value;

                    if (id !== msg.id) {
                      return; // not for us!
                    }
                    element.innerHTML = value;
                    if (element.localName === "pre") {
                      element.classList.add("hljs");
                    }
                    if (language) {
                      element.classList.add(language);
                    }
                    clearTimeout(timeoutId);
                    _worker.worker.removeEventListener("message", listener);
                    done();
                  });
                });
              });
              _context.next = 8;
              return Promise.all(promisesToHighlight);

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

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