"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var text_selection_listener_exports = {};
__export(text_selection_listener_exports, {
  addTextSelectionListener: () => addTextSelectionListener
});
module.exports = __toCommonJS(text_selection_listener_exports);
const getSelectedText = (selection) => selection.toString().replace(/(\r\n|\r|\n){2,}/g, " ");
const getInnerSelectionNodes = (range) => {
  const _iterator = document.createNodeIterator(range.commonAncestorContainer, NodeFilter.SHOW_ALL);
  const innerSelectionNodes = [];
  while (_iterator.nextNode() && _iterator.referenceNode !== range.startContainer) {
  }
  while (_iterator.nextNode() && _iterator.referenceNode !== range.endContainer) {
    if (_iterator.referenceNode.nodeType === Node.TEXT_NODE) {
      innerSelectionNodes.push(_iterator.referenceNode);
    }
  }
  return innerSelectionNodes;
};
const getSelectionRects = (selection) => {
  const range = selection.getRangeAt(0);
  const innerSelectionNodes = getInnerSelectionNodes(range);
  const innerRects = innerSelectionNodes.map((n) => {
    const r = document.createRange();
    r.selectNodeContents(n);
    return r.getBoundingClientRect();
  });
  const clientRects = range.getClientRects();
  return [clientRects[0], ...innerRects, clientRects[clientRects.length - 1]];
};
const getSelectionPosition = (selection) => {
  const rects = getSelectionRects(selection);
  let left = 999999;
  let right = 0;
  rects.forEach((rect) => {
    left = Math.min(left, rect.left);
    right = Math.max(right, rect.right);
  });
  const y = rects[0].top + window.scrollY;
  const x = (left + right) / 2 + window.scrollX;
  return { x, y };
};
function addTextSelectionListener(container, callback) {
  const handleSelection = () => {
    const selection = document.getSelection();
    const selectionAnchor = selection?.anchorNode;
    const selectionFocus = selection?.focusNode;
    let selectedText, position;
    if (selection && selection.rangeCount > 0 && !selection.isCollapsed && container.contains(selectionAnchor) && container.contains(selectionFocus)) {
      selectedText = getSelectedText(selection);
      position = getSelectionPosition(selection);
    }
    callback(selectedText, position);
  };
  document.addEventListener("selectionchange", handleSelection);
  return () => document.removeEventListener("selectionchange", handleSelection);
}
//# sourceMappingURL=text-selection-listener.js.map
