var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React, { useCallback, useRef, useState } from 'react';
import classnames from 'classnames';
import { identity } from 'ramda';
import Button from '@mui/material/Button';
import Menu from '@mui/material/Menu';
import { DefaultMenuItemRenderer } from './components/DefaultMenuItemRenderer';
import { useDidUpdateEffect } from '../hooks/useDidUpdateEffect';
import { useStyles } from './styles';
export function DropDownMenuButton(_a) {
    var _b;
    var className = _a.className, menuId = _a.menuId, _c = _a.buttonComponent, buttonComponent = _c === void 0 ? Button : _c, buttonContent = _a.buttonContent, _d = _a.menuItems, menuItems = _d === void 0 ? [] : _d, _e = _a.buttonProps, buttonProps = _e === void 0 ? {} : _e, _f = _a.popoverProps, popoverProps = _f === void 0 ? {} : _f, _g = _a.onMenuOpen, onMenuOpen = _g === void 0 ? identity : _g, _h = _a.onMenuClose, onMenuClose = _h === void 0 ? identity : _h, _j = _a.MenuItemRenderer, MenuItemRenderer = _j === void 0 ? DefaultMenuItemRenderer : _j;
    var ButtonComponent = buttonComponent;
    var styles = useStyles();
    var buttonRef = useRef();
    var _k = useState(false), isOpen = _k[0], setIsOpen = _k[1];
    useDidUpdateEffect(function () {
        if (isOpen) {
            onMenuOpen();
        }
        else {
            onMenuClose();
        }
    }, [isOpen]);
    var handleToggle = useCallback(function (e) {
        e === null || e === void 0 ? void 0 : e.stopPropagation();
        setIsOpen(function (open) { return !open; });
    }, []);
    var renderMenuItem = function (item, index) {
        return React.createElement(MenuItemRenderer, { item: item, key: index, onMenuClose: handleToggle });
    };
    return (React.createElement(React.Fragment, null,
        React.createElement(ButtonComponent, __assign({ "data-reltio-id": "drop-down-menu-button", ref: buttonRef, className: classnames(styles.buttonRoot, (_b = {},
                _b[styles.selected] = isOpen,
                _b), className), onClick: handleToggle, "aria-pressed": isOpen, "aria-controls": isOpen ? menuId : undefined, "aria-haspopup": "true" }, buttonProps), buttonContent),
        React.createElement(Menu, __assign({ id: menuId, variant: "menu", open: isOpen, autoFocus: false, classes: { paper: styles.paper }, anchorEl: buttonRef.current, onClose: handleToggle, anchorOrigin: {
                vertical: 'bottom',
                horizontal: 'right'
            }, transformOrigin: {
                vertical: 'top',
                horizontal: 'right'
            }, elevation: 2, MenuListProps: { autoFocusItem: isOpen } }, popoverProps), menuItems.map(renderMenuItem))));
}
