import { useCallback, useContext, useEffect, useMemo, } from 'react';
import { InfiniteTreeListContext } from '../../InfiniteTreeListContext';
import { TreeListItemContext } from '../../InfiniteTreeListItem';
export const useLogic = ({ actions }) => {
    const { main, secondary = [] } = actions;
    const { setDisabledReason, item } = useContext(TreeListItemContext);
    const { setDisabled } = useContext(InfiniteTreeListContext);
    const blockingAction = useMemo(() => [...main, ...secondary].find((action) => action.isBlockingOperation && action.loading), [main, secondary]);
    const isDisabledAction = Boolean(blockingAction);
    useEffect(() => {
        if (blockingAction) {
            // Добавляем id элемента в Set disabled элементов
            setDisabled(item.id, true);
            setDisabledReason(blockingAction?.loadingNote);
            return;
        }
        // Удаляем id элемента из Set disabled элементов
        setDisabled(item.id, false);
        setDisabledReason('');
    }, [blockingAction, item.id]);
    const handleActionClick = useCallback((onClick) => () => {
        onClick?.(item);
    }, [item]);
    const handleWrapperClick = (event) => {
        event.stopPropagation();
    };
    return { isDisabledAction, handleActionClick, handleWrapperClick };
};
