function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import nullthrows from 'nullthrows';
import uuid from './ui/uuid.js';

// Utility Class that allows developer to insert HTML snippets then updates
// document's innerHTML accordingly.
var HTMLMutator = /*#__PURE__*/function () {
  function HTMLMutator(doc) {
    _classCallCheck(this, HTMLMutator);
    _defineProperty(this, "_doc", void 0);
    _defineProperty(this, "_htmls", void 0);
    this._doc = doc;
    this._htmls = new Map();
  }
  return _createClass(HTMLMutator, [{
    key: "insertHTMLBefore",
    value: function insertHTMLBefore(html, el) {
      return this._insertHTML(html, 'before', el);
    }
  }, {
    key: "insertHTMLAfter",
    value: function insertHTMLAfter(html, el) {
      return this._insertHTML(html, 'after', el);
    }
  }, {
    key: "execute",
    value: function execute() {
      var doc = this._doc;
      var root = nullthrows(doc.body || doc.documentElement);
      var newHtml = root.innerHTML;
      this._htmls.forEach(function (html, token) {
        newHtml = newHtml.replace(token, html);
      });
      root.innerHTML = newHtml;
    }
  }, {
    key: "_insertHTML",
    value: function _insertHTML(html, position, el) {
      if (el.ownerDocument !== this._doc) {
        throw new Error('element does not belong to the document');
      }
      // This does not insert the HTML into the document directly.
      // Instead, this inserts a comment token that can be replaced by the HTML
      // later.
      var token = "\u200B_HTMLMutator_token_".concat(uuid(), "_\u200B");
      var node = this._doc.createComment(token);
      var parentElement = nullthrows(el.parentElement);
      if (position === 'before') {
        parentElement.insertBefore(node, el);
      } else if (position === 'after') {
        parentElement.insertBefore(node, el.nextSibling);
      } else {
        throw new Error("Invalid position ".concat(position));
      }
      this._htmls.set('<!--' + token + '-->', html);
      return this;
    }
  }]);
}();
export { HTMLMutator as default };