import { __assign, __rest } from "tslib";
import React, { useContext, useState } from 'react';
import classNames from 'classnames';
import { MenuContext } from './menu-context';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
var Menu = function (props) {
    var _a = props.defaultIndex, defaultIndex = _a === void 0 ? '0' : _a, _b = props.mode, mode = _b === void 0 ? 'horizontal' : _b, _c = props.theme, theme = _c === void 0 ? 'light' : _c, _d = props.inlineIndent, inlineIndent = _d === void 0 ? 20 : _d, onSelect = props.onSelect, className = props.className, overlayClassName = props.overlayClassName, style = props.style, children = props.children, customisedCls = props.prefixCls, otherProps = __rest(props, ["defaultIndex", "mode", "theme", "inlineIndent", "onSelect", "className", "overlayClassName", "style", "children", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('menu', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className, prefixCls + "_root", prefixCls + "_" + theme, prefixCls + "_" + mode);
    var _e = useState(defaultIndex), activeIndex = _e[0], setActiveIndex = _e[1];
    var contextValue = {
        mode: mode,
        inlineIndent: inlineIndent,
        index: activeIndex,
        onSelect: function (index) {
            setActiveIndex(index);
            onSelect && onSelect(index);
        },
    };
    return (React.createElement("ul", __assign({}, otherProps, { role: "menu", className: cls, style: style }),
        React.createElement(MenuContext.Provider, { value: contextValue }, React.Children.map(children, function (child, index) {
            var childElement = child;
            var displayName = childElement.type.displayName;
            if (displayName === 'MenuItem' ||
                displayName === 'SubMenu' ||
                displayName === 'MenuItemGroup' ||
                (displayName === 'MenuDivider' && mode !== 'horizontal')) {
                var childProps = {
                    overlayClassName: overlayClassName,
                    index: "" + index,
                };
                return React.cloneElement(childElement, childProps);
            }
            else {
                console.warn('Menu has a child that is not a MenuItem component.');
                return null;
            }
        }))));
};
Menu.displayName = 'Menu';
export default Menu;
//# sourceMappingURL=menu.js.map