"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);

// packages/block-editor/src/components/writing-flow/use-drag-selection.js
var use_drag_selection_exports = {};
__export(use_drag_selection_exports, {
  default: () => useDragSelection
});
module.exports = __toCommonJS(use_drag_selection_exports);
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_store = require("../../store");
function setContentEditableWrapper(node, value) {
  node.contentEditable = value;
  if (value) {
    node.focus();
  }
}
function useDragSelection() {
  const { startMultiSelect, stopMultiSelect } = (0, import_data.useDispatch)(import_store.store);
  const {
    isSelectionEnabled,
    hasSelectedBlock,
    isDraggingBlocks,
    isMultiSelecting
  } = (0, import_data.useSelect)(import_store.store);
  return (0, import_compose.useRefEffect)(
    (node) => {
      const { ownerDocument } = node;
      const { defaultView } = ownerDocument;
      let anchorElement;
      let rafId;
      function onMouseUp() {
        stopMultiSelect();
        defaultView.removeEventListener("mouseup", onMouseUp);
        rafId = defaultView.requestAnimationFrame(() => {
          if (!hasSelectedBlock()) {
            return;
          }
          setContentEditableWrapper(node, false);
          const selection = defaultView.getSelection();
          if (selection.rangeCount) {
            const range = selection.getRangeAt(0);
            const { commonAncestorContainer } = range;
            const clonedRange = range.cloneRange();
            if (anchorElement.contains(commonAncestorContainer)) {
              anchorElement.focus();
              selection.removeAllRanges();
              selection.addRange(clonedRange);
            }
          }
        });
      }
      let lastMouseDownTarget;
      function onMouseDown({ target }) {
        lastMouseDownTarget = target;
      }
      function onMouseLeave({ buttons, target, relatedTarget }) {
        if (!target.contains(lastMouseDownTarget)) {
          return;
        }
        if (target.contains(relatedTarget)) {
          return;
        }
        if (isDraggingBlocks()) {
          return;
        }
        if (buttons !== 1) {
          return;
        }
        if (isMultiSelecting()) {
          return;
        }
        if (node === target) {
          return;
        }
        if (target.getAttribute("contenteditable") !== "true") {
          return;
        }
        if (!isSelectionEnabled()) {
          return;
        }
        anchorElement = target;
        startMultiSelect();
        defaultView.addEventListener("mouseup", onMouseUp);
        setContentEditableWrapper(node, true);
      }
      node.addEventListener("mouseout", onMouseLeave);
      node.addEventListener("mousedown", onMouseDown);
      return () => {
        node.removeEventListener("mouseout", onMouseLeave);
        defaultView.removeEventListener("mouseup", onMouseUp);
        defaultView.cancelAnimationFrame(rafId);
      };
    },
    [
      startMultiSelect,
      stopMultiSelect,
      isSelectionEnabled,
      hasSelectedBlock
    ]
  );
}
//# sourceMappingURL=use-drag-selection.js.map
