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