import { useCallback } from 'react';
import { useCanvasStore } from '@/lib/store/canvasStore';
import { AppNodeData, AppNodeType } from '../types/node';

export function useNodeData(id: string) {
    const update = useCallback((data: Partial<AppNodeData>) => {
        // Directly get current state and create entirely new references
        // This bypasses immer to ensure ReactFlow detects the nodes change
        const currentNodes = useCanvasStore.getState().nodes;
        const newNodes = currentNodes.map(n => {
            if (n.id === id) {
                return { ...n, data: { ...n.data, ...data } } as AppNodeType;
            }
            return n;
        });
        useCanvasStore.setState({ nodes: newNodes, isDirty: true } as any);
    }, [id]);

    return {
        update
    };
}
