// (C) 2007-2025 GoodData Corporation
import { useCallback, useMemo, useState } from "react";
export function MenuState({ opened, defaultOpened = false, onOpenedChange, children }) {
    const isControlled = useMemo(() => {
        return typeof opened === "boolean";
    }, [opened]);
    const [internalOpened, setInternalOpened] = useState(() => {
        return typeof opened === "boolean" ? opened : defaultOpened;
    });
    const handleOpenedChange = useCallback((openedChangeParams) => {
        setInternalOpened(openedChangeParams.opened);
        if (onOpenedChange) {
            onOpenedChange(openedChangeParams);
        }
    }, [onOpenedChange]);
    return children({
        opened: (isControlled ? opened : internalOpened) ?? false,
        onOpenedChange: handleOpenedChange,
    });
}
