'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const core_esm = require('../../../../../../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.cjs');
const dndContextColissionUtils = require('./dnd-context-colission-utils.cjs');
const dndUtils = require('./dnd-utils.cjs');
const dndContexts = require('./dnd-contexts.cjs');
const dragOverlay = require('./drag-overlay.cjs');
const useAutoScroll = require('../hooks/use-auto-scroll.cjs');
const useDndSensors = require('../hooks/use-dnd-sensors.cjs');
const useDndEventHandlers = require('../hooks/use-dnd-event-handlers.cjs');
const useDropValidation = require('../hooks/use-drop-validation.cjs');
const useKeyboardShortcutsDnd = require('../hooks/use-keyboard-shortcuts-dnd.cjs');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }

const React__default = /*#__PURE__*/_interopDefaultCompat(React);

const DndContextProvider = ({ children }) => {
  const [activeLayerId, setActiveLayerId] = React.useState(null);
  const [newComponentType, setNewComponentType] = React.useState(null);
  const [componentDragging, setComponentDragging] = React.useState(false);
  const { handleParentMouseMove, handleIframeMouseMove, stopAutoScroll } = useAutoScroll.useAutoScroll();
  const sensors = useDndSensors.useDndSensors();
  const clearDragState = React.useCallback(() => {
    setActiveLayerId(null);
    setNewComponentType(null);
  }, []);
  const canDropOnLayerRef = React__default.useRef(() => false);
  const { handleDragStart, handleDragEnd, handleDragCancel, isLayerDescendantOf } = useDndEventHandlers.useDndEventHandlers({
    stopAutoScroll,
    setActiveLayerId,
    setNewComponentType,
    clearDragState,
    canDropOnLayer: (layerId) => canDropOnLayerRef.current(layerId)
  });
  const { canDropOnLayer } = useDropValidation.useDropValidation(activeLayerId, isLayerDescendantOf, newComponentType);
  React__default.useEffect(() => {
    canDropOnLayerRef.current = canDropOnLayer;
  }, [canDropOnLayer]);
  const handleKeyboardCancel = React.useCallback(() => {
    handleDragCancel();
    clearDragState();
  }, [handleDragCancel, clearDragState]);
  useKeyboardShortcutsDnd.useKeyboardShortcutsDnd(activeLayerId || newComponentType, handleKeyboardCancel);
  const collisionDetection = dndContextColissionUtils.createTransformAwareCollisionDetection();
  const contextValue = React.useMemo(() => ({
    isDragging: !!activeLayerId || !!newComponentType,
    activeLayerId,
    newComponentType,
    canDropOnLayer
  }), [activeLayerId, newComponentType, canDropOnLayer]);
  const componentDragContextValue = React.useMemo(() => ({
    isDragging: componentDragging,
    setDragging: setComponentDragging
  }), [componentDragging]);
  const isDragging = !!activeLayerId || !!newComponentType;
  React.useEffect(() => {
    if (isDragging) {
      const handleParentMove = (event) => handleParentMouseMove(event, true);
      document.addEventListener("mousemove", handleParentMove);
      const iframeElements = dndUtils.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]);
  React.useEffect(() => {
    return () => {
      stopAutoScroll();
    };
  }, [stopAutoScroll]);
  return /* @__PURE__ */ jsxRuntime.jsx(dndContexts.DndContextStateContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxRuntime.jsx(dndContexts.ComponentDragContext.Provider, { value: componentDragContextValue, children: /* @__PURE__ */ jsxRuntime.jsxs(
    core_esm.DndContext,
    {
      sensors,
      collisionDetection,
      onDragStart: handleDragStart,
      onDragEnd: handleDragEnd,
      onDragCancel: handleDragCancel,
      children: [
        children,
        /* @__PURE__ */ jsxRuntime.jsx(dragOverlay.TransformAwareDragOverlay, { children: activeLayerId || newComponentType ? /* @__PURE__ */ jsxRuntime.jsx(
          dragOverlay.DragOverlayContent,
          {
            layerId: activeLayerId || void 0,
            componentType: newComponentType || void 0
          }
        ) : null })
      ]
    }
  ) }) });
};

exports.useComponentDragContext = dndContexts.useComponentDragContext;
exports.useDndContext = dndContexts.useDndContext;
exports.DndContextProvider = DndContextProvider;
