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

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.run = exports.done = 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;
  var swapStyleOwner = (0, _utils.makeOwnerSwapper)(codeStyle);
  swapStyleOwner(document.head);

  function getLanguageHint(classList) {
    return Array.from(classList).filter(function (item) {
      return item !== "highlight" && item !== "nolinks";
    }).map(function (item) {
      return item.toLowerCase();
    });
  }
  var doneResolver = void 0;
  var doneRejector = void 0;
  var done = exports.done = new Promise(function (resolve, reject) {
    doneResolver = resolve;
    doneRejector = reject;
  });

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

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

            case 3:

              if (codeStyle.ownerDocument !== doc) {
                swapStyleOwner(doc.head);
              }

              if (doc.querySelector(".highlight")) {
                (0, _pubsubhub.pub)("warn", "pre elements don't need a 'highlight' class anymore.");
              }

              promisesToHighlight = Array.from(doc.querySelectorAll("pre:not(.idl):not(.nohighlight),code.highlight")).map(function (element) {
                element.setAttribute("aria-busy", "true");
                return element;
              }).map(function (element) {
                return new Promise(function (resolve, reject) {
                  if (element.textContent.trim() === "") {
                    return resolve(element); // no work to do
                  }
                  var msg = {
                    action: "highlight",
                    code: element.textContent,
                    id: Math.random().toString(),
                    languages: getLanguageHint(element.classList)
                  };
                  element.setAttribute("aria-live", "polite");
                  _worker.worker.postMessage(msg);
                  _worker.worker.addEventListener("message", function listener(ev) {
                    if (ev.data.id !== msg.id) {
                      return; // not for us!
                    }
                    _worker.worker.removeEventListener("message", listener);
                    var _ev$data = ev.data,
                        value = _ev$data.value,
                        language = _ev$data.language;

                    element.innerHTML = value;
                    if (element.localName === "pre") {
                      element.classList.add("hljs");
                      element.classList.add(language);
                    }
                    resolve(element);
                  });
                  setTimeout(function () {
                    element.setAttribute("aria-busy", "false");
                    var errMsg = "Timeout error trying to process: " + msg.code;
                    var err = new Error(errMsg);
                    reject(err);
                  }, 5000);
                });
              });
              _context.prev = 6;
              _context.next = 9;
              return Promise.all(promisesToHighlight);

            case 9:
              tranformedElements = _context.sent;

              tranformedElements.forEach(function (element) {
                return element.setAttribute("aria-busy", "false");
              });
              doneResolver();
              _context.next = 18;
              break;

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

              console.error(_context.t0);
              doneRejector(_context.t0);

            case 18:
              cb();

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

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