import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2026 GoodData Corporation
import { useCallback, useEffect, useRef } from "react";
import { MenuPosition } from "../positioning/MenuPosition.js";
export function MenuOpenedByHover({ onOpenedChange, toggler, togglerWrapperClassName, opened, topLevelMenu, alignment, spacing, offset, portalTarget, children, }) {
    const openCloseDelayMs = 200;
    const timerCloseDelay = useRef(null);
    const clearCloseDelayTimer = useCallback(() => {
        if (timerCloseDelay.current) {
            window.clearTimeout(timerCloseDelay.current);
        }
    }, []);
    useEffect(() => {
        return () => {
            clearCloseDelayTimer();
        };
    }, [clearCloseDelayTimer]);
    const hoverStart = useCallback(() => {
        clearCloseDelayTimer();
        timerCloseDelay.current = window.setTimeout(() => {
            onOpenedChange({ opened: true, source: "HOVER_TIMEOUT" });
        }, openCloseDelayMs);
    }, [clearCloseDelayTimer, onOpenedChange]);
    const hoverEnd = useCallback(() => {
        clearCloseDelayTimer();
        timerCloseDelay.current = window.setTimeout(() => {
            onOpenedChange({ opened: false, source: "HOVER_TIMEOUT" });
        }, openCloseDelayMs);
    }, [clearCloseDelayTimer, onOpenedChange]);
    return (_jsx(MenuPosition, { toggler: _jsx("div", { onMouseEnter: hoverStart, onMouseLeave: hoverEnd, children: toggler }), togglerWrapperClassName: togglerWrapperClassName, opened: opened, topLevelMenu: topLevelMenu, alignment: alignment, spacing: spacing, offset: offset, portalTarget: portalTarget, children: _jsx("div", { onMouseEnter: hoverStart, onMouseLeave: hoverEnd, children: children }) }));
}
