import { useState, useRef, useCallback } from 'react';

interface UseHoverPanelProps<T> {
    openDelay?: number;
    closeDelay?: number;
    defaultActive?: T | null;
}

export function useHoverPanel<T = string>({ 
    openDelay = 0, 
    closeDelay = 0,
    defaultActive = null
}: UseHoverPanelProps<T> = {}) {
    const [activePanel, setActivePanel] = useState<T | null>(defaultActive);
    const closeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
    const openTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);

    // Legacy hover handler - now just clears timeouts
    const handleHover = useCallback((id: T) => {
        // No-op for click-only interaction
    }, []);

    // Legacy leave handler - now just clears timeouts
    const handleLeave = useCallback(() => {
       // No-op for click-only interaction
    }, []);

    const handlePanelEnter = useCallback(() => {
        // No-op
    }, []);

    const handlePanelLeave = useCallback(() => {
        // No-op
    }, []);

    // Force close immediately
    const closeImmediately = useCallback(() => {
        setActivePanel(null);
    }, []);

    const togglePanel = useCallback((id: T) => {
        setActivePanel(prev => prev === id ? null : id);
    }, []);

    return {
        activePanel,
        setActivePanel,
        handleHover, // Deprecated but kept for compatibility
        handleLeave, // Deprecated but kept for compatibility
        handlePanelEnter, // Deprecated
        handlePanelLeave, // Deprecated
        closeImmediately,
        togglePanel
    };
}
