import React, { createContext, useContext, useRef, useCallback } from 'react';

interface PointerTrackingContextType {
    activePointersRef: React.MutableRefObject<Map<number, string | undefined>>;
    addPointer: (id: number, cardId?: string) => void;
    removePointer: (id: number) => void;
}

const PointerTrackingContext = createContext<PointerTrackingContextType | null>(null);

export const PointerTrackingProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
    const activePointersRef = useRef(new Map<number, string | undefined>());

    const addPointer = useCallback((id: number, cardId?: string) => {
        activePointersRef.current.set(id, cardId);
    }, []);

    const removePointer = useCallback((id: number) => {
        activePointersRef.current.delete(id);
    }, []);

    return React.createElement(
        PointerTrackingContext.Provider,
        { value: { activePointersRef, addPointer, removePointer } },
        children
    );
};

export const usePointerTracking = () => {
    const context = useContext(PointerTrackingContext);
    if (!context) {
        throw new Error('usePointerTracking must be used within a PointerTrackingProvider');
    }
    return context;
};
