import { useCallback } from 'react';
import { Node, Edge } from 'reactflow';

interface UseLockingProps {
  nodes: Node[];
  edges: Edge[];
  selectedNode: Node | null;
  selectedEdge: Edge | null;
}

export const useLocking = ({
  nodes,
  edges,
  selectedNode,
  selectedEdge,
}: UseLockingProps) => {
  const isNodeLocked = useCallback((nodeId: string): boolean => {
    const node = nodes.find(n => n.id === nodeId);
    return node?.data?.locked === true;
  }, [nodes]);

  const isEdgeLocked = useCallback((edgeId: string): boolean => {
    const edge = edges.find(e => e.id === edgeId);
    return edge?.data?.locked === true;
  }, [edges]);

  const canDeleteSelected = useCallback((): boolean => {
    if (selectedNode && selectedNode.data?.locked) return false;
    if (selectedEdge && selectedEdge.data?.locked) return false;
    return selectedNode !== null || selectedEdge !== null;
  }, [selectedNode, selectedEdge]);

  return {
    isNodeLocked,
    isEdgeLocked,
    canDeleteSelected,
  };
};