"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { forwardRef } from "react";
import { Button } from "../button/Button.js";
import { IconRotator } from "../icon/IconRotator.js";
import { getIcon } from "../icon/config.js";
import { useAppSize } from "../media-queries/AppSizeProvider.js";
import { useEnsuredId } from "../useEnsuredId.js";
import { useMenuConfiguration } from "./MenuConfigurationProvider.js";
import { useMenuVisibility } from "./MenuVisibilityProvider.js";
const noop = ()=>{
// do nothing
};
/**
 * **Client Component**
 *
 * An internal component that handles rendering a button for the `DropdownMenu`
 * while implementing the correct accessibility and keyboard movement.
 *
 * @see {@link https://react-md.dev/components/menu | Menu Demos}
 * @since 5.0.0
 * @since 6.0.0 See {@link BaseMenuButtonProps} for breaking changes.
 */ export const MenuButton = /*#__PURE__*/ forwardRef(function MenuButton(props, ref) {
    const { id: propId, onClick = noop, onKeyDown = noop, icon: propIcon, iconAfter = true, iconRotatorProps, floating, buttonType = floating ? "icon" : "text", disableDropdownIcon = buttonType === "icon", children, ...remaining } = props;
    const id = useEnsuredId(propId, "menubutton");
    const { visible, setVisible, defaultFocusIndex } = useMenuVisibility();
    const { renderAsSheet } = useMenuConfiguration();
    const { isPhone } = useAppSize();
    const isSheet = renderAsSheet === true || renderAsSheet === "phone" && isPhone;
    const dropdownIcon = getIcon("dropdown", propIcon);
    let icon = propIcon;
    if (!disableDropdownIcon) {
        icon = /*#__PURE__*/ _jsx(IconRotator, {
            ...iconRotatorProps,
            rotated: visible,
            children: dropdownIcon
        });
    }
    return /*#__PURE__*/ _jsxs(Button, {
        ...remaining,
        "aria-haspopup": isSheet ? "dialog" : "menu",
        "aria-expanded": visible || undefined,
        id: id,
        ref: ref,
        floating: floating,
        buttonType: buttonType,
        onClick: (event)=>{
            onClick(event);
            defaultFocusIndex.current = 0;
            setVisible((prevVisible)=>!prevVisible);
        },
        onKeyDown: (event)=>{
            onKeyDown(event);
            switch(event.key){
                case "ArrowDown":
                    event.preventDefault();
                    event.stopPropagation();
                    defaultFocusIndex.current = 0;
                    setVisible(true);
                    break;
                case "ArrowUp":
                    event.preventDefault();
                    event.stopPropagation();
                    defaultFocusIndex.current = -1;
                    setVisible(true);
                    break;
            }
        },
        children: [
            !iconAfter && icon,
            children,
            iconAfter && icon
        ]
    });
});

//# sourceMappingURL=MenuButton.js.map