import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2025 GoodData Corporation
import { useCallback, useEffect } from "react";
import { MenuOpener } from "./menuOpener/MenuOpener.js";
export function ControlledMenu({ opened, openAction, closeOnScroll, portalTarget, onOpenedChange, toggler, togglerWrapperClassName, children, alignment, spacing, offset, }) {
    const closeMenu = useCallback(() => {
        onOpenedChange({ opened: false, source: "SCROLL" });
    }, [onOpenedChange]);
    const addScrollListeners = useCallback(() => {
        window.addEventListener("scroll", closeMenu, true);
    }, [closeMenu]);
    const removeScrollListeners = useCallback(() => {
        window.removeEventListener("scroll", closeMenu, true);
    }, [closeMenu]);
    useEffect(() => {
        if (closeOnScroll) {
            addScrollListeners();
        }
        return () => {
            if (closeOnScroll) {
                removeScrollListeners();
            }
        };
    }, [closeOnScroll, addScrollListeners, removeScrollListeners]);
    return (_jsx(MenuOpener, { opened: opened, onOpenedChange: onOpenedChange, openAction: openAction, alignment: alignment, spacing: spacing, offset: offset, portalTarget: portalTarget, toggler: toggler, togglerWrapperClassName: togglerWrapperClassName, topLevelMenu: true, children: children }));
}
