"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const InfiniteTreeListContext_1 = require("../../InfiniteTreeListContext");
const TreeListItemContext_1 = require("../TreeListItemContext");
const useLogic = ({ isDisabled, disabledReason, }) => {
    const { itemSkeleton, setDisabled } = (0, react_1.useContext)(InfiniteTreeListContext_1.InfiniteTreeListContext);
    const { item, onToggle, isDisabled: isDisabledByActions, disabledReason: disabledReasonByActions, } = (0, react_1.useContext)(TreeListItemContext_1.TreeListItemContext);
    (0, react_1.useEffect)(() => {
        if (isDisabled) {
            setDisabled(item.id, isDisabled);
        }
    }, [isDisabled]);
    const [isChevronActive, setChevronActive] = (0, react_1.useState)(item.meta.isExpanded);
    const handleClick = (0, react_1.useCallback)((event) => {
        event.stopPropagation();
        setChevronActive((prev) => !prev);
        onToggle(item);
    }, [item, onToggle]);
    // Omit ref для избежания ошибки типов у Tooltip
    const tooltipProps = {
        title: (isDisabled && disabledReason) ||
            (isDisabledByActions && disabledReasonByActions),
        withoutContainer: !isDisabled || !isDisabledByActions,
        placement: 'bottom',
        arrow: false,
        followCursor: true,
    };
    return {
        item,
        handleClick,
        itemSkeleton,
        isDisabled,
        disabledReason,
        tooltipProps,
        isDisabledByActions,
        isChevronActive,
    };
};
exports.useLogic = useLogic;
