"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const index = require("../../../../../_vendor/usehooks-ts/dist-index.cjs");
const INDICATOR_CLASS = "drag-drop-indicator pointer-events-none absolute right-0 left-0 z-20";
const DEFAULT_SCROLL_EDGE_SIZE = 40;
const DEFAULT_SCROLL_SPEED = 10;
function createDropIndicator(position) {
  const indicator = document.createElement("div");
  indicator.className = INDICATOR_CLASS;
  indicator.style.position = "absolute";
  indicator.style.left = "0";
  indicator.style.right = "0";
  indicator.style.zIndex = "20";
  indicator.style.pointerEvents = "none";
  if (position === "top") {
    indicator.style.top = "-1px";
  } else {
    indicator.style.bottom = "-1px";
  }
  const line = document.createElement("div");
  line.style.height = "2px";
  line.style.width = "100%";
  line.style.backgroundColor = "#3b82f6";
  line.style.borderRadius = "1px";
  indicator.appendChild(line);
  return indicator;
}
function usePanelDragDrop({
  onDrop,
  autoScroll = true,
  scrollEdgeSize = DEFAULT_SCROLL_EDGE_SIZE,
  scrollSpeed = DEFAULT_SCROLL_SPEED
} = {}) {
  const dragStateRef = React.useRef({
    isDragging: false,
    dragItemId: null,
    dropTargetId: null,
    dropPosition: null
  });
  const lastValidRef = React.useRef({
    id: null,
    position: null
  });
  const mousePositionRef = React.useRef({ x: 0, y: 0 });
  const autoScrollRAFRef = React.useRef(null);
  const scrollContainerRef = React.useRef(null);
  const [isDragging, setIsDragging] = React.useState(false);
  const [dragItemId, setDragItemId] = React.useState(null);
  const containerRef = React.useRef(null);
  const clearDropIndicators = index.useEventCallback(() => {
    document.querySelectorAll(".drag-drop-indicator").forEach((indicator) => {
      indicator.remove();
    });
    document.querySelectorAll('[data-drop-target="true"]').forEach((element) => {
      const htmlElement = element;
      htmlElement.dataset.dropTarget = "false";
      htmlElement.dataset.dropPosition = "";
    });
  });
  const findScrollContainer = index.useEventCallback(() => {
    if (scrollContainerRef.current) return scrollContainerRef.current;
    if (containerRef.current) {
      let parent = containerRef.current.parentElement;
      while (parent) {
        const hasVerticalScroll = parent.scrollHeight > parent.clientHeight;
        const style = window.getComputedStyle(parent);
        const isScrollable = style.overflowY === "auto" || style.overflowY === "scroll";
        if (hasVerticalScroll && isScrollable) {
          scrollContainerRef.current = parent;
          return parent;
        }
        parent = parent.parentElement;
      }
    }
    return null;
  });
  const handleAutoScroll = index.useEventCallback(() => {
    if (!isDragging || !autoScroll) return;
    const scrollContainer = findScrollContainer();
    if (!scrollContainer) return;
    const containerRect = scrollContainer.getBoundingClientRect();
    const { y } = mousePositionRef.current;
    const distanceToTop = y - containerRect.top;
    const distanceToBottom = containerRect.bottom - y;
    let scrollAmount = 0;
    if (distanceToTop < scrollEdgeSize && distanceToTop >= 0) {
      const scrollFactor = 1 - distanceToTop / scrollEdgeSize;
      scrollAmount = -Math.round(scrollSpeed * scrollFactor);
    } else if (distanceToBottom < scrollEdgeSize && distanceToBottom >= 0) {
      const scrollFactor = 1 - distanceToBottom / scrollEdgeSize;
      scrollAmount = Math.round(scrollSpeed * scrollFactor);
    }
    if (scrollAmount !== 0) {
      scrollContainer.scrollTop += scrollAmount;
      autoScrollRAFRef.current = requestAnimationFrame(handleAutoScroll);
    } else {
      autoScrollRAFRef.current = null;
    }
  });
  const stopAutoScroll = index.useEventCallback(() => {
    if (autoScrollRAFRef.current !== null) {
      cancelAnimationFrame(autoScrollRAFRef.current);
      autoScrollRAFRef.current = null;
    }
  });
  const endDragRef = React.useRef();
  const handleDragOver = index.useEventCallback(
    (currentElement, currentMouseY) => {
      const rect = currentElement.getBoundingClientRect();
      const rowId = currentElement.dataset.rowId;
      if (!rowId) return { targetElement: null, dropPosition: null };
      const relY = currentMouseY - rect.top;
      const dropPosition = relY < rect.height * 0.5 ? "top" : "bottom";
      return {
        targetElement: currentElement,
        dropPosition
      };
    }
  );
  const handleGlobalMouseMove = index.useEventCallback((e) => {
    if (!dragStateRef.current.isDragging || !containerRef.current) {
      return;
    }
    mousePositionRef.current = { x: e.clientX, y: e.clientY };
    if (autoScroll) {
      stopAutoScroll();
      autoScrollRAFRef.current = requestAnimationFrame(handleAutoScroll);
    }
    const allFoundElements = Array.from(
      containerRef.current.querySelectorAll(".queue-sortable-row")
    );
    const rowElements = allFoundElements.filter(
      (el) => el.dataset.rowId && el.dataset.rowId.trim() !== ""
    );
    if (rowElements.length === 0) return;
    let identifiedTargetElement = null;
    let identifiedDropPosition = null;
    for (let i = 0; i < rowElements.length; i++) {
      const rowElement = rowElements[i];
      const rect = rowElement.getBoundingClientRect();
      const rowId = rowElement.dataset.rowId;
      const isCurrentDraggedItem = rowId === dragStateRef.current.dragItemId;
      const isTheActualLastItemInDom = i === rowElements.length - 1;
      const currentMouseY = mousePositionRef.current.y;
      const currentMouseX = mousePositionRef.current.x;
      const detectionPadding = rect.height * 0.25;
      const isMouseVerticallyOver = currentMouseY >= rect.top - detectionPadding && currentMouseY <= rect.bottom + detectionPadding;
      const isMouseHorizontallyOver = currentMouseX >= rect.left && currentMouseX <= rect.right;
      if (isMouseVerticallyOver && isMouseHorizontallyOver) {
        if (isCurrentDraggedItem) {
          const isFirstItem = i === 0;
          const relY = currentMouseY - rect.top;
          if (isFirstItem && relY < rect.height * 0.5) {
            identifiedTargetElement = rowElement;
            identifiedDropPosition = "top";
            break;
          }
          if (isTheActualLastItemInDom && relY >= rect.height * 0.5) {
            identifiedTargetElement = rowElement;
            identifiedDropPosition = "bottom";
            break;
          }
          continue;
        } else {
          const { targetElement, dropPosition } = handleDragOver(rowElement, currentMouseY);
          identifiedTargetElement = targetElement;
          identifiedDropPosition = dropPosition;
          break;
        }
      }
    }
    const identifiedRowId = identifiedTargetElement ? identifiedTargetElement.dataset.rowId : null;
    if (identifiedTargetElement && identifiedRowId && identifiedDropPosition) {
      if (dragStateRef.current.dropTargetId !== identifiedRowId || dragStateRef.current.dropPosition !== identifiedDropPosition) {
        clearDropIndicators();
        dragStateRef.current.dropTargetId = identifiedRowId;
        dragStateRef.current.dropPosition = identifiedDropPosition;
        lastValidRef.current = { id: identifiedRowId, position: identifiedDropPosition };
        identifiedTargetElement.dataset.dropTarget = "true";
        identifiedTargetElement.dataset.dropPosition = identifiedDropPosition;
        const indicator = createDropIndicator(identifiedDropPosition);
        identifiedTargetElement.appendChild(indicator);
      }
    } else if (dragStateRef.current.dropTargetId !== null) {
      dragStateRef.current.dropTargetId = null;
      dragStateRef.current.dropPosition = null;
    }
  });
  const handleGlobalMouseUp = index.useEventCallback(() => {
    if (endDragRef.current) {
      endDragRef.current();
    }
  });
  const endDrag = index.useEventCallback(() => {
    if (!dragStateRef.current.isDragging) return;
    stopAutoScroll();
    clearDropIndicators();
    if (dragStateRef.current.dragItemId && lastValidRef.current.id && lastValidRef.current.position && onDrop && dragStateRef.current.dragItemId !== lastValidRef.current.id) {
      onDrop(
        dragStateRef.current.dragItemId,
        lastValidRef.current.id,
        lastValidRef.current.position
      );
    }
    dragStateRef.current = {
      isDragging: false,
      dragItemId: null,
      dropTargetId: null,
      dropPosition: null
    };
    lastValidRef.current = {
      id: null,
      position: null
    };
    setIsDragging(false);
    setDragItemId(null);
    document.removeEventListener("mousemove", handleGlobalMouseMove);
    document.removeEventListener("mouseup", handleGlobalMouseUp);
  });
  React.useEffect(() => {
    endDragRef.current = endDrag;
    return () => {
      if (dragStateRef.current.isDragging) {
        document.removeEventListener("mousemove", handleGlobalMouseMove);
        document.removeEventListener("mouseup", handleGlobalMouseUp);
        clearDropIndicators();
      }
      stopAutoScroll();
    };
  }, [endDrag, handleGlobalMouseMove, handleGlobalMouseUp, clearDropIndicators, stopAutoScroll]);
  const handleDragStart = index.useEventCallback((item, e) => {
    e.preventDefault();
    e.stopPropagation();
    clearDropIndicators();
    mousePositionRef.current = { x: e.clientX, y: e.clientY };
    lastValidRef.current = {
      id: null,
      position: null
    };
    if (autoScroll) {
      findScrollContainer();
    }
    dragStateRef.current = {
      isDragging: true,
      dragItemId: item.id,
      dropTargetId: null,
      dropPosition: null
    };
    setIsDragging(true);
    setDragItemId(item.id);
    document.addEventListener("mousemove", handleGlobalMouseMove);
    document.addEventListener("mouseup", handleGlobalMouseUp);
  });
  return {
    containerRef,
    isDragging,
    isItemBeingDragged: React.useCallback(
      (id) => isDragging && dragItemId === id,
      [isDragging, dragItemId]
    ),
    isDropTarget: React.useCallback((id) => dragStateRef.current.dropTargetId === id, []),
    getDropPosition: React.useCallback(
      (id) => dragStateRef.current.dropTargetId === id ? dragStateRef.current.dropPosition : null,
      []
    ),
    handleDragStart
  };
}
exports.usePanelDragDrop = usePanelDragDrop;
//# sourceMappingURL=use-message-queue-drag.cjs.map
