import { useCallback } from 'react';
import { useLayerStore } from '../store/layer-store.mjs';
import { useEditorStore } from '../store/editor-store.mjs';
import { duplicateWithNewIdsAndName } from '../store/layer-utils.mjs';
import { canPasteLayer } from '../utils/paste-validation.mjs';

function useGlobalLayerActions(layerId) {
  const findLayerById = useLayerStore((state) => state.findLayerById);
  const removeLayer = useLayerStore((state) => state.removeLayer);
  const duplicateLayer = useLayerStore((state) => state.duplicateLayer);
  const addLayerDirect = useLayerStore((state) => state.addLayerDirect);
  const isLayerAPage = useLayerStore((state) => state.isLayerAPage);
  const componentRegistry = useEditorStore((state) => state.registry);
  const allowPagesCreation = useEditorStore((state) => state.allowPagesCreation);
  const allowPagesDeletion = useEditorStore((state) => state.allowPagesDeletion);
  const setClipboard = useEditorStore((state) => state.setClipboard);
  const clearClipboard = useEditorStore((state) => state.clearClipboard);
  const getEffectiveLayerId = useCallback(
    () => layerId ?? useLayerStore.getState().selectedLayerId,
    [layerId]
  );
  const handleCopy = useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const layer = findLayerById(effectiveLayerId);
    if (!layer) return;
    const clonedLayer = duplicateWithNewIdsAndName(layer, false);
    setClipboard({
      layer: clonedLayer,
      isCut: false,
      sourceLayerId: effectiveLayerId
    });
  }, [getEffectiveLayerId, findLayerById, setClipboard]);
  const handleCut = useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const layer = findLayerById(effectiveLayerId);
    if (!layer) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesDeletion) return;
    const clonedLayer = duplicateWithNewIdsAndName(layer, false);
    setClipboard({
      layer: clonedLayer,
      isCut: true,
      sourceLayerId: effectiveLayerId
    });
    removeLayer(effectiveLayerId);
  }, [getEffectiveLayerId, findLayerById, isLayerAPage, allowPagesDeletion, removeLayer, setClipboard]);
  const handlePaste = useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const currentClipboard = useEditorStore.getState().clipboard;
    if (!currentClipboard.layer) return;
    if (!canPasteLayer(currentClipboard.layer, effectiveLayerId, componentRegistry, findLayerById)) {
      return;
    }
    const layerToAdd = duplicateWithNewIdsAndName(currentClipboard.layer, false);
    addLayerDirect(layerToAdd, effectiveLayerId);
    if (currentClipboard.isCut) {
      clearClipboard();
    }
  }, [getEffectiveLayerId, componentRegistry, findLayerById, addLayerDirect, clearClipboard]);
  const handleDelete = useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesDeletion) return;
    removeLayer(effectiveLayerId);
  }, [getEffectiveLayerId, isLayerAPage, allowPagesDeletion, removeLayer]);
  const handleDuplicate = useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesCreation) return;
    duplicateLayer(effectiveLayerId);
  }, [getEffectiveLayerId, isLayerAPage, allowPagesCreation, duplicateLayer]);
  const getCanPaste = useCallback(
    () => {
      const effectiveLayerId = getEffectiveLayerId();
      if (!effectiveLayerId) return false;
      const { clipboard } = useEditorStore.getState();
      if (!clipboard.layer) return false;
      return canPasteLayer(clipboard.layer, effectiveLayerId, componentRegistry, findLayerById);
    },
    [getEffectiveLayerId, componentRegistry, findLayerById]
  );
  const effectiveLayerIdForPermissions = getEffectiveLayerId();
  const isPage = effectiveLayerIdForPermissions ? isLayerAPage(effectiveLayerIdForPermissions) : false;
  const canDuplicate = !isPage || allowPagesCreation;
  const canDelete = !isPage || allowPagesDeletion;
  const canCut = canDelete;
  return {
    getCanPaste,
    canDuplicate,
    canDelete,
    canCut,
    handleCopy,
    handleCut,
    handlePaste,
    handleDelete,
    handleDuplicate
  };
}

export { useGlobalLayerActions };
