import { useCallback, useEffect, useMemo, useRef, useState } from "react";
const useToolbarState = ({ config, expanded, nodeId, hidden })=>{
    const [isDropdownOpen, setIsDropdownOpen] = useState(false);
    const dropdownRef = useRef(null);
    const buttonRef = useRef(null);
    const actionsWithState = useMemo(()=>config.actions.map((action)=>{
            if ('separator' === action.id) return action;
            const item = action;
            return {
                ...item,
                onClick: ()=>{
                    try {
                        item.onAction(nodeId);
                    } catch (error) {
                        console.error(`Error executing action ${item.id}:`, error);
                    }
                }
            };
        }), [
        config.actions,
        nodeId
    ]);
    const pinnedActions = useMemo(()=>actionsWithState.filter((action)=>'separator' !== action.id && !!action.isPinned), [
        actionsWithState
    ]);
    const overflowActionsWithState = useMemo(()=>{
        if (!config.overflowActions) return [];
        return config.overflowActions.map((action)=>{
            if ('separator' === action.id) return action;
            const item = action;
            return {
                ...item,
                onClick: ()=>{
                    try {
                        item.onAction(nodeId);
                    } catch (error) {
                        console.error(`Error executing action ${item.id}:`, error);
                    }
                }
            };
        });
    }, [
        config.overflowActions,
        nodeId
    ]);
    const displayState = useMemo(()=>{
        if (hidden) return 'hidden';
        if (expanded) return 'expanded';
        return pinnedActions.length > 0 ? 'pinned' : 'hidden';
    }, [
        hidden,
        expanded,
        pinnedActions.length
    ]);
    const actionsToDisplay = useMemo(()=>{
        if ('hidden' === displayState) return [];
        if ('expanded' === displayState) return actionsWithState;
        return pinnedActions;
    }, [
        displayState,
        actionsWithState,
        pinnedActions
    ]);
    const shouldShowOverflow = 'expanded' === displayState && overflowActionsWithState.length > 0;
    useEffect(()=>{
        const handleClickOutside = (event)=>{
            if (dropdownRef.current && !dropdownRef.current.contains(event.target) && buttonRef.current && !buttonRef.current.contains(event.target)) setIsDropdownOpen(false);
        };
        if (isDropdownOpen) document.addEventListener('mousedown', handleClickOutside);
        return ()=>document.removeEventListener('mousedown', handleClickOutside);
    }, [
        isDropdownOpen
    ]);
    useEffect(()=>{
        if (!shouldShowOverflow && isDropdownOpen) setIsDropdownOpen(false);
    }, [
        shouldShowOverflow,
        isDropdownOpen
    ]);
    const toggleDropdown = useCallback((e)=>{
        e.stopPropagation();
        e.preventDefault();
        setIsDropdownOpen((prev)=>!prev);
    }, []);
    return {
        isDropdownOpen,
        setIsDropdownOpen,
        dropdownRef,
        buttonRef,
        shouldShowOverflow,
        displayState,
        actionsToDisplay,
        overflowActionsToDisplay: overflowActionsWithState,
        separatorOrientation: config.position && 'top' !== config.position && 'bottom' !== config.position ? 'horizontal' : 'vertical',
        toggleDropdown
    };
};
export { useToolbarState };
