function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import { ConfigProvider, Spin } from "antd";
import classNames from "clsx";
import React, { useContext } from "react";
import { useRefFunction } from "../Hooks/useRefFunction";
import { useStyle } from "./style";
// 菜单项组件
var MenuItem = function MenuItem1(param) {
    var item = param.item, isSelected = param.isSelected, _param_inlineIndent = param.inlineIndent, inlineIndent = _param_inlineIndent === void 0 ? 0 : _param_inlineIndent, onSelect = param.onSelect, _param_level = param.level, level = _param_level === void 0 ? 0 : _param_level, prefixCls = param.prefixCls, hashId = param.hashId, currentSelectedKey = param.currentSelectedKey, customClassNames = param.classNames;
    var baseClass = "".concat(prefixCls, "-item");
    var handleClick = useRefFunction(function() {
        if (!item.disabled) {
            var _item_onClick;
            onSelect(item.key);
            (_item_onClick = item.onClick) === null || _item_onClick === void 0 ? void 0 : _item_onClick.call(item);
        }
    });
    // 分组类型但子项为空时不展示
    if (item.type === 'group' && (!item.children || !item.children.length)) {
        return null;
    }
    // 如果是分组且有子项，并且嵌套层级小于2
    if (item.type === 'group' && item.children && level < 2) {
        return /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(prefixCls, "-submenu"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.submenuClassName)
        }, /*#__PURE__*/ React.createElement("div", {
            className: classNames(hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemClassName, _define_property({}, "".concat(baseClass, "-group"), true))
        }, /*#__PURE__*/ React.createElement("div", {
            className: classNames("".concat(baseClass, "-content"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemContentClassName)
        }, item.icon && /*#__PURE__*/ React.createElement("span", {
            className: classNames("".concat(baseClass, "-icon"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemIconClassName)
        }, item.icon), item.label)), item.children.map(function(child) {
            return /*#__PURE__*/ React.createElement(MenuItem, {
                key: child.key,
                item: child,
                isSelected: currentSelectedKey === child.key,
                inlineIndent: inlineIndent,
                onSelect: onSelect,
                level: level + 1,
                prefixCls: prefixCls,
                hashId: hashId,
                currentSelectedKey: currentSelectedKey,
                classNames: customClassNames
            });
        }));
    }
    var _obj;
    return /*#__PURE__*/ React.createElement("div", {
        className: classNames(baseClass, hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemClassName, (_obj = {}, _define_property(_obj, "".concat(baseClass, "-selected"), isSelected), _define_property(_obj, "".concat(baseClass, "-disabled"), item.disabled), _obj)),
        onClick: handleClick,
        role: "menuitem",
        tabIndex: item.disabled ? -1 : 0,
        "aria-selected": isSelected,
        "aria-disabled": item.disabled
    }, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(baseClass, "-content"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemContentClassName)
    }, item.icon && /*#__PURE__*/ React.createElement("span", {
        className: classNames("".concat(baseClass, "-icon"), hashId, customClassNames === null || customClassNames === void 0 ? void 0 : customClassNames.menuItemIconClassName)
    }, item.icon), item.label));
};
/**
 * GroupMenu 组件 - 兼容 Antd Menu API 的自定义菜单组件
 */ export var GroupMenu = function GroupMenu(props) {
    var _props_items = props.items, items = _props_items === void 0 ? [] : _props_items, _props_selectedKeys = props.selectedKeys, selectedKeys = _props_selectedKeys === void 0 ? [] : _props_selectedKeys, onSelect = props.onSelect, _props_inlineIndent = props.inlineIndent, inlineIndent = _props_inlineIndent === void 0 ? 20 : _props_inlineIndent, className = props.className, tmp = props.classNames, propsClassNames = tmp === void 0 ? {} : tmp, style = props.style, _props_loading = props.loading, loading = _props_loading === void 0 ? false : _props_loading, restProps = _object_without_properties(props, [
        "items",
        "selectedKeys",
        "onSelect",
        "inlineIndent",
        "className",
        "classNames",
        "style",
        "loading"
    ]);
    // 使用 ConfigProvider 获取前缀类名
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var prefixCls = getPrefixCls('agentic-chat-history-menu');
    // 注册样式
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    // 确定当前选中的键
    var currentSelectedKey = selectedKeys && selectedKeys[0];
    // 直接使用传入的 items，支持最多双层嵌套
    var dataSource = items || [];
    var handleSelect = useRefFunction(function(key) {
        onSelect === null || onSelect === void 0 ? void 0 : onSelect({
            key: key
        });
    });
    // 直接传递用户自定义的 classNames
    return wrapSSR(/*#__PURE__*/ React.createElement("div", _object_spread({
        className: classNames(prefixCls, hashId, className),
        style: style,
        role: "menu",
        "aria-label": "菜单",
        tabIndex: 0
    }, restProps), loading ? /*#__PURE__*/ React.createElement("div", {
        style: {
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
            height: '100%'
        }
    }, /*#__PURE__*/ React.createElement(Spin, null)) : dataSource.map(function(item) {
        return /*#__PURE__*/ React.createElement(MenuItem, {
            key: item.key,
            item: item,
            isSelected: currentSelectedKey === item.key,
            inlineIndent: inlineIndent,
            onSelect: handleSelect,
            classNames: propsClassNames,
            prefixCls: prefixCls,
            hashId: hashId,
            currentSelectedKey: currentSelectedKey
        });
    })));
};
export default GroupMenu;
