"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactUtils = require("@contember/react-utils");
const react = require("react");
const Slate = require("slate");
const slateReact = require("slate-react");
const editorSelectionReducer = require("./editorSelectionReducer.cjs");
const useEditorSelection = (maxInterval = 100) => {
  const editor = slateReact.useSlateStatic();
  const [selectionState, dispatch] = react.useReducer(editorSelectionReducer.editorSelectionReducer, editorSelectionReducer.defaultEditorSelectionState);
  const selectionStateRef = react.useRef(selectionState);
  selectionStateRef.current = selectionState;
  const onDOMSelectionChange = reactUtils.useDebounceCallback(
    react.useCallback(() => {
      const domSelection = getSelection();
      const isRelevant = domSelection && domSelection.anchorNode && slateReact.ReactEditor.hasDOMNode(editor, domSelection.anchorNode, { editable: true });
      if (isRelevant) {
        if ((selectionStateRef.current.name === "expandedPointerSelection" || selectionStateRef.current.name === "expandedNonPointerSelection") && !domSelection.isCollapsed && selectionStateRef.current.selection) {
          const stateSelectionRange = slateReact.ReactEditor.toSlateRange(editor, selectionStateRef.current.selection, { exactMatch: false, suppressThrow: false });
          const domSelectionRange = slateReact.ReactEditor.toSlateRange(editor, domSelection, { exactMatch: false, suppressThrow: false });
          if (Slate.Range.equals(stateSelectionRange, domSelectionRange) && (!editor.selection || Slate.Editor.string(editor, editor.selection) === selectionStateRef.current.selectedString)) {
            return;
          }
        }
        dispatch({
          type: "setSelection",
          selection: domSelection
        });
      } else {
        dispatch({
          type: "blur"
        });
      }
    }, [editor]),
    maxInterval
  );
  const onMouseDown = react.useCallback(
    (e) => {
      e.target && e.target instanceof Node && slateReact.ReactEditor.hasDOMNode(editor, e.target) && dispatch({
        type: "setMousePointerSelectionStart",
        event: e
      });
    },
    [editor]
  );
  const onMouseUp = react.useCallback(
    (e) => {
      const relevantTarget = !!e.target && e.target instanceof Node && slateReact.ReactEditor.hasDOMNode(editor, e.target);
      dispatch({
        type: "setMousePointerSelectionFinish",
        event: relevantTarget ? e : void 0
      });
    },
    [editor]
  );
  react.useEffect(() => {
    document.addEventListener("selectionchange", onDOMSelectionChange);
    document.addEventListener("mousedown", onMouseDown);
    document.addEventListener("mouseup", onMouseUp);
    return () => {
      document.removeEventListener("selectionchange", onDOMSelectionChange);
      document.removeEventListener("mousedown", onMouseDown);
      document.removeEventListener("mouseup", onMouseUp);
    };
  }, [onDOMSelectionChange, onMouseDown, onMouseUp]);
  return selectionState;
};
exports.useEditorSelection = useEditorSelection;
//# sourceMappingURL=useEditorSelection.cjs.map
