// packages/block-editor/src/components/block-draggable/use-scroll-when-dragging.js
import { getScrollContainer } from "@wordpress/dom";
import { useCallback, useEffect, useRef } from "@wordpress/element";
var SCROLL_INACTIVE_DISTANCE_PX = 50;
var SCROLL_INTERVAL_MS = 25;
var PIXELS_PER_SECOND_PER_PERCENTAGE = 1e3;
var VELOCITY_MULTIPLIER = PIXELS_PER_SECOND_PER_PERCENTAGE * (SCROLL_INTERVAL_MS / 1e3);
function useScrollWhenDragging() {
  const dragStartYRef = useRef(null);
  const velocityYRef = useRef(null);
  const scrollParentYRef = useRef(null);
  const scrollEditorIntervalRef = useRef(null);
  useEffect(
    () => () => {
      if (scrollEditorIntervalRef.current) {
        clearInterval(scrollEditorIntervalRef.current);
        scrollEditorIntervalRef.current = null;
      }
    },
    []
  );
  const startScrolling = useCallback((event) => {
    dragStartYRef.current = event.clientY;
    scrollParentYRef.current = getScrollContainer(event.target);
    scrollEditorIntervalRef.current = setInterval(() => {
      if (scrollParentYRef.current && velocityYRef.current) {
        const newTop = scrollParentYRef.current.scrollTop + velocityYRef.current;
        scrollParentYRef.current.scroll({
          top: newTop
        });
      }
    }, SCROLL_INTERVAL_MS);
  }, []);
  const scrollOnDragOver = useCallback((event) => {
    if (!scrollParentYRef.current) {
      return;
    }
    const scrollParentHeight = scrollParentYRef.current.offsetHeight;
    const offsetDragStartPosition = dragStartYRef.current - scrollParentYRef.current.offsetTop;
    const offsetDragPosition = event.clientY - scrollParentYRef.current.offsetTop;
    if (event.clientY > offsetDragStartPosition) {
      const moveableDistance = Math.max(
        scrollParentHeight - offsetDragStartPosition - SCROLL_INACTIVE_DISTANCE_PX,
        0
      );
      const dragDistance = Math.max(
        offsetDragPosition - offsetDragStartPosition - SCROLL_INACTIVE_DISTANCE_PX,
        0
      );
      const distancePercentage = moveableDistance === 0 || dragDistance === 0 ? 0 : dragDistance / moveableDistance;
      velocityYRef.current = VELOCITY_MULTIPLIER * distancePercentage;
    } else if (event.clientY < offsetDragStartPosition) {
      const moveableDistance = Math.max(
        offsetDragStartPosition - SCROLL_INACTIVE_DISTANCE_PX,
        0
      );
      const dragDistance = Math.max(
        offsetDragStartPosition - offsetDragPosition - SCROLL_INACTIVE_DISTANCE_PX,
        0
      );
      const distancePercentage = moveableDistance === 0 || dragDistance === 0 ? 0 : dragDistance / moveableDistance;
      velocityYRef.current = -VELOCITY_MULTIPLIER * distancePercentage;
    } else {
      velocityYRef.current = 0;
    }
  }, []);
  const stopScrolling = () => {
    dragStartYRef.current = null;
    scrollParentYRef.current = null;
    if (scrollEditorIntervalRef.current) {
      clearInterval(scrollEditorIntervalRef.current);
      scrollEditorIntervalRef.current = null;
    }
  };
  return [startScrolling, scrollOnDragOver, stopScrolling];
}
export {
  useScrollWhenDragging as default
};
//# sourceMappingURL=use-scroll-when-dragging.js.map
