"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const useLogic = ({ id, prefixId, isDefaultExpanded = false, isForceExpanded, isDisabled, disabledReason, onClick, }) => {
    const [isOpen, setOpen] = (0, react_1.useState)(isDefaultExpanded);
    (0, react_1.useEffect)(() => {
        setOpen(isDefaultExpanded);
    }, []);
    (0, react_1.useEffect)(() => {
        if (typeof isForceExpanded === 'boolean') {
            setOpen(isForceExpanded);
        }
    }, [isForceExpanded]);
    const handleClick = (event) => {
        if (isDisabled) {
            return;
        }
        if (event.type === 'click') {
            return onClick?.(event);
        }
        if (event.type === 'keydown' &&
            event.code === 'Enter') {
            return onClick?.(event);
        }
    };
    const handleToggle = (event) => {
        event.stopPropagation();
        setOpen((currentState) => !currentState);
    };
    const idAttr = prefixId ? `${prefixId}${id}` : id;
    const tabIndex = isDisabled ? -1 : 0;
    return {
        isOpen,
        handleToggle,
        handleClick,
        itemProps: {
            id: idAttr,
        },
        itemContentProps: {
            tabIndex,
            onClick: handleClick,
            onKeyDown: handleClick,
        },
        tooltipProps: {
            title: disabledReason,
            withoutContainer: !isDisabled,
            disableInteractive: true,
        },
    };
};
exports.useLogic = useLogic;
