'use strict';

const React = require('react');
const layerStore = require('../store/layer-store.cjs');
const editorStore = require('../store/editor-store.cjs');
const layerUtils = require('../store/layer-utils.cjs');
const pasteValidation = require('../utils/paste-validation.cjs');

function useGlobalLayerActions(layerId) {
  const findLayerById = layerStore.useLayerStore((state) => state.findLayerById);
  const removeLayer = layerStore.useLayerStore((state) => state.removeLayer);
  const duplicateLayer = layerStore.useLayerStore((state) => state.duplicateLayer);
  const addLayerDirect = layerStore.useLayerStore((state) => state.addLayerDirect);
  const isLayerAPage = layerStore.useLayerStore((state) => state.isLayerAPage);
  const componentRegistry = editorStore.useEditorStore((state) => state.registry);
  const allowPagesCreation = editorStore.useEditorStore((state) => state.allowPagesCreation);
  const allowPagesDeletion = editorStore.useEditorStore((state) => state.allowPagesDeletion);
  const setClipboard = editorStore.useEditorStore((state) => state.setClipboard);
  const clearClipboard = editorStore.useEditorStore((state) => state.clearClipboard);
  const getEffectiveLayerId = React.useCallback(
    () => layerId ?? layerStore.useLayerStore.getState().selectedLayerId,
    [layerId]
  );
  const handleCopy = React.useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const layer = findLayerById(effectiveLayerId);
    if (!layer) return;
    const clonedLayer = layerUtils.duplicateWithNewIdsAndName(layer, false);
    setClipboard({
      layer: clonedLayer,
      isCut: false,
      sourceLayerId: effectiveLayerId
    });
  }, [getEffectiveLayerId, findLayerById, setClipboard]);
  const handleCut = React.useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const layer = findLayerById(effectiveLayerId);
    if (!layer) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesDeletion) return;
    const clonedLayer = layerUtils.duplicateWithNewIdsAndName(layer, false);
    setClipboard({
      layer: clonedLayer,
      isCut: true,
      sourceLayerId: effectiveLayerId
    });
    removeLayer(effectiveLayerId);
  }, [getEffectiveLayerId, findLayerById, isLayerAPage, allowPagesDeletion, removeLayer, setClipboard]);
  const handlePaste = React.useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const currentClipboard = editorStore.useEditorStore.getState().clipboard;
    if (!currentClipboard.layer) return;
    if (!pasteValidation.canPasteLayer(currentClipboard.layer, effectiveLayerId, componentRegistry, findLayerById)) {
      return;
    }
    const layerToAdd = layerUtils.duplicateWithNewIdsAndName(currentClipboard.layer, false);
    addLayerDirect(layerToAdd, effectiveLayerId);
    if (currentClipboard.isCut) {
      clearClipboard();
    }
  }, [getEffectiveLayerId, componentRegistry, findLayerById, addLayerDirect, clearClipboard]);
  const handleDelete = React.useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesDeletion) return;
    removeLayer(effectiveLayerId);
  }, [getEffectiveLayerId, isLayerAPage, allowPagesDeletion, removeLayer]);
  const handleDuplicate = React.useCallback(() => {
    const effectiveLayerId = getEffectiveLayerId();
    if (!effectiveLayerId) return;
    const isPage2 = isLayerAPage(effectiveLayerId);
    if (isPage2 && !allowPagesCreation) return;
    duplicateLayer(effectiveLayerId);
  }, [getEffectiveLayerId, isLayerAPage, allowPagesCreation, duplicateLayer]);
  const getCanPaste = React.useCallback(
    () => {
      const effectiveLayerId = getEffectiveLayerId();
      if (!effectiveLayerId) return false;
      const { clipboard } = editorStore.useEditorStore.getState();
      if (!clipboard.layer) return false;
      return pasteValidation.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
  };
}

exports.useGlobalLayerActions = useGlobalLayerActions;
