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(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
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); }
var EMPTY_SELECTION_VALUE = Object.freeze({
  from: 0,
  to: 0
});
function resolveSelectionValue(el) {
  if (!window.getSelection) {
    console.warn('window.getSelection() is not supported');
    return EMPTY_SELECTION_VALUE;
  }
  var selection = window.getSelection();
  if (!selection.containsNode) {
    console.warn('selection.containsNode() is not supported');
    return EMPTY_SELECTION_VALUE;
  }
  if (!selection.rangeCount) {
    return EMPTY_SELECTION_VALUE;
  }
  var range = selection.getRangeAt(0);
  if (!range) {
    return EMPTY_SELECTION_VALUE;
  }
  var startContainer = range.startContainer,
    endContainer = range.endContainer,
    startOffset = range.startOffset,
    endOffset = range.endOffset;
  if (startContainer === el || endContainer === el || startContainer && el.contains(startContainer) || endContainer && el.contains(endContainer)) {
    return {
      from: startOffset,
      to: endOffset
    };
  }
  return EMPTY_SELECTION_VALUE;
}
var SelectionObserver = /*#__PURE__*/function () {
  function SelectionObserver(_callback) {
    var _this = this;
    _classCallCheck(this, SelectionObserver);
    _defineProperty(this, "_observables", []);
    _defineProperty(this, "_callback", null);
    _defineProperty(this, "_onClick", function () {
      var callback = _this._callback;
      _this._observables = _this._observables.map(function (obj) {
        var target = obj.target;
        return {
          target: target,
          selection: resolveSelectionValue(target)
        };
      });
      callback === null || callback === void 0 || callback(_this.takeRecords(), _this);
    });
    _defineProperty(this, "_check", function () {
      var changed = false;
      var callback = _this._callback;
      _this._observables = _this._observables.map(function (obj) {
        var target = obj.target,
          selection = obj.selection;
        var $selection = resolveSelectionValue(target);
        if (selection === $selection) {
          return obj;
        }
        if (selection.from === $selection.from && selection.to === $selection.to) {
          return obj;
        }
        changed = true;
        return {
          target: target,
          selection: $selection
        };
      });
      changed && (callback === null || callback === void 0 ? void 0 : callback(_this.takeRecords(), _this));
    });
    this._callback = _callback;
  }
  return _createClass(SelectionObserver, [{
    key: "disconnect",
    value: function disconnect() {
      var _this2 = this;
      this._observables.forEach(function (obj) {
        var el = obj.target;
        el.removeEventListener('click', _this2._check, false);
        el.removeEventListener('selectionchange', _this2._check, false);
      });
      this._observables = [];
    }
  }, {
    key: "observe",
    value: function observe(el) {
      if (!window.getSelection) {
        console.warn('window.getSelection() is not supported');
        return;
      }
      if (this._observables.some(function (obj) {
        return obj.target === el;
      })) {
        // Already observed.
        return;
      }
      var obj = {
        target: el,
        selection: resolveSelectionValue(el)
      };
      el.addEventListener('click', this._check, false);
      el.addEventListener('selectionchange', this._check, false);
      this._observables.push(obj);
    }
  }, {
    key: "takeRecords",
    value: function takeRecords() {
      return this._observables.slice(0);
    }
  }]);
}();
export { SelectionObserver as default };