import { jsx, jsxs } from 'react/jsx-runtime';
import React__default, { useState, useCallback, useMemo, useEffect } from 'react';
import { DndContext } from '../../../../../../node_modules/.pnpm/@dnd-kit_core@6.3.1_react-dom@19.2.0_react@19.2.0__react@19.2.0/node_modules/@dnd-kit/core/dist/core.esm.mjs';
import { createTransformAwareCollisionDetection } from './dnd-context-colission-utils.mjs';
import { getIframeElements } from './dnd-utils.mjs';
import { DndContextStateContext, ComponentDragContext } from './dnd-contexts.mjs';
export { useComponentDragContext, useDndContext } from './dnd-contexts.mjs';
import { TransformAwareDragOverlay, DragOverlayContent } from './drag-overlay.mjs';
import { useAutoScroll } from '../hooks/use-auto-scroll.mjs';
import { useDndSensors } from '../hooks/use-dnd-sensors.mjs';
import { useDndEventHandlers } from '../hooks/use-dnd-event-handlers.mjs';
import { useDropValidation } from '../hooks/use-drop-validation.mjs';
import { useKeyboardShortcutsDnd } from '../hooks/use-keyboard-shortcuts-dnd.mjs';

const DndContextProvider = ({ children }) => {
  const [activeLayerId, setActiveLayerId] = useState(null);
  const [newComponentType, setNewComponentType] = useState(null);
  const [componentDragging, setComponentDragging] = useState(false);
  const { handleParentMouseMove, handleIframeMouseMove, stopAutoScroll } = useAutoScroll();
  const sensors = useDndSensors();
  const clearDragState = useCallback(() => {
    setActiveLayerId(null);
    setNewComponentType(null);
  }, []);
  const canDropOnLayerRef = React__default.useRef(() => false);
  const { handleDragStart, handleDragEnd, handleDragCancel, isLayerDescendantOf } = useDndEventHandlers({
    stopAutoScroll,
    setActiveLayerId,
    setNewComponentType,
    clearDragState,
    canDropOnLayer: (layerId) => canDropOnLayerRef.current(layerId)
  });
  const { canDropOnLayer } = useDropValidation(activeLayerId, isLayerDescendantOf, newComponentType);
  React__default.useEffect(() => {
    canDropOnLayerRef.current = canDropOnLayer;
  }, [canDropOnLayer]);
  const handleKeyboardCancel = useCallback(() => {
    handleDragCancel();
    clearDragState();
  }, [handleDragCancel, clearDragState]);
  useKeyboardShortcutsDnd(activeLayerId || newComponentType, handleKeyboardCancel);
  const collisionDetection = createTransformAwareCollisionDetection();
  const contextValue = useMemo(() => ({
    isDragging: !!activeLayerId || !!newComponentType,
    activeLayerId,
    newComponentType,
    canDropOnLayer
  }), [activeLayerId, newComponentType, canDropOnLayer]);
  const componentDragContextValue = useMemo(() => ({
    isDragging: componentDragging,
    setDragging: setComponentDragging
  }), [componentDragging]);
  const isDragging = !!activeLayerId || !!newComponentType;
  useEffect(() => {
    if (isDragging) {
      const handleParentMove = (event) => handleParentMouseMove(event, true);
      document.addEventListener("mousemove", handleParentMove);
      const iframeElements = getIframeElements();
      let iframeCleanup = null;
      if (iframeElements) {
        const { window: iframeWindow } = iframeElements;
        if (iframeWindow) {
          const handleIframeMove = (event) => handleIframeMouseMove(event, true);
          iframeWindow.addEventListener("mousemove", handleIframeMove);
          iframeCleanup = () => {
            try {
              iframeWindow.removeEventListener("mousemove", handleIframeMove);
            } catch (error) {
              console.warn("Failed to remove iframe mouse listener:", error);
            }
          };
        }
      }
      return () => {
        document.removeEventListener("mousemove", handleParentMove);
        if (iframeCleanup) {
          iframeCleanup();
        }
        stopAutoScroll();
      };
    } else {
      stopAutoScroll();
    }
  }, [isDragging, handleParentMouseMove, handleIframeMouseMove, stopAutoScroll]);
  useEffect(() => {
    return () => {
      stopAutoScroll();
    };
  }, [stopAutoScroll]);
  return /* @__PURE__ */ jsx(DndContextStateContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(ComponentDragContext.Provider, { value: componentDragContextValue, children: /* @__PURE__ */ jsxs(
    DndContext,
    {
      sensors,
      collisionDetection,
      onDragStart: handleDragStart,
      onDragEnd: handleDragEnd,
      onDragCancel: handleDragCancel,
      children: [
        children,
        /* @__PURE__ */ jsx(TransformAwareDragOverlay, { children: activeLayerId || newComponentType ? /* @__PURE__ */ jsx(
          DragOverlayContent,
          {
            layerId: activeLayerId || void 0,
            componentType: newComponentType || void 0
          }
        ) : null })
      ]
    }
  ) }) });
};

export { DndContextProvider };
