import { useCallback, useEffect, useMemo, useState } from 'react';
export const useLogic = ({ item: [, { items, active: externalActive }], }) => {
    const [isOpen, setIsOpen] = useState(items?.some(([, { active }]) => {
        return active;
    }));
    const isGroup = Boolean(items?.length);
    useEffect(() => {
        const hasActiveChild = items?.some(([, { active }]) => active);
        if (hasActiveChild) {
            setIsOpen(true);
        }
    }, [items]);
    //Если элемент раскрывается, то он не может быть selected
    const selected = useMemo(() => {
        return isGroup ? !isOpen && externalActive : externalActive;
    }, [isGroup, isOpen, externalActive]);
    const handleClick = useCallback(() => {
        setIsOpen((prevValue) => {
            if (typeof prevValue === 'boolean') {
                return !prevValue;
            }
            return false;
        });
    }, []);
    const isGroupTitleItem = Boolean(items?.length);
    return { selected, handleClick, isOpen, isGroupTitleItem };
};
