'use strict';

const React = require('react');
const layerStore = require('../store/layer-store.cjs');
const layerUtils = require('../store/layer-utils.cjs');

const useDndEventHandlers = ({
  stopAutoScroll,
  setActiveLayerId,
  setNewComponentType,
  clearDragState,
  canDropOnLayer
}) => {
  const moveLayer = layerStore.useLayerStore((state) => state.moveLayer);
  const addComponentLayer = layerStore.useLayerStore((state) => state.addComponentLayer);
  const pages = layerStore.useLayerStore((state) => state.pages);
  const isLayerDescendantOf = React.useCallback((childId, parentId) => {
    if (childId === parentId) return true;
    const parentLayers = layerUtils.findAllParentLayersRecursive(pages, childId);
    return parentLayers.some((parent) => parent.id === parentId);
  }, [pages]);
  const handleDragStart = React.useCallback((event) => {
    const { active } = event;
    if (active.data.current?.type === "layer") {
      setActiveLayerId(active.data.current.layerId);
    } else if (active.data.current?.type === "new-component") {
      const componentType = active.data.current.componentType;
      if (componentType && setNewComponentType) {
        setNewComponentType(componentType);
      }
    }
  }, [setActiveLayerId, setNewComponentType]);
  const handleDragEnd = React.useCallback((event) => {
    const { active, over } = event;
    stopAutoScroll();
    const activeData = active.data.current;
    const overData = over?.data.current;
    if (!over || !overData || overData.type !== "drop-zone") {
      if (clearDragState) {
        clearDragState();
      } else {
        setActiveLayerId(null);
      }
      return;
    }
    const targetParentId = overData.parentId;
    const targetPosition = overData.position;
    if (canDropOnLayer && !canDropOnLayer(targetParentId)) {
      if (clearDragState) {
        clearDragState();
      } else {
        setActiveLayerId(null);
      }
      return;
    }
    if (activeData?.type === "layer" && activeData.layerId) {
      const activeLayerId = activeData.layerId;
      if (isLayerDescendantOf(targetParentId, activeLayerId)) {
        if (clearDragState) {
          clearDragState();
        } else {
          setActiveLayerId(null);
        }
        return;
      }
      moveLayer(activeLayerId, targetParentId, targetPosition);
    } else if (activeData?.type === "new-component" && activeData.componentType) {
      addComponentLayer(activeData.componentType, targetParentId, targetPosition);
    }
    if (clearDragState) {
      clearDragState();
    } else {
      setActiveLayerId(null);
    }
  }, [moveLayer, addComponentLayer, isLayerDescendantOf, stopAutoScroll, setActiveLayerId, clearDragState, canDropOnLayer]);
  const handleDragCancel = React.useCallback(() => {
    stopAutoScroll();
    if (clearDragState) {
      clearDragState();
    } else {
      setActiveLayerId(null);
    }
  }, [stopAutoScroll, setActiveLayerId, clearDragState]);
  return {
    handleDragStart,
    handleDragEnd,
    handleDragCancel,
    isLayerDescendantOf
  };
};

exports.useDndEventHandlers = useDndEventHandlers;
