/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import pick from 'lodash/pick';
import { SubMenu as SubMenu$2 } from 'rc-menu';
import { createObject, autobind, themeable } from 'amis-core';
import { getIcon, Icon } from '../icons.js';
import { Badge } from '../Badge.js';
import { MenuContext } from './MenuContext.js';

/**
 * @file SubMenu
 * @description 导航子菜单
 * @author fex
 */
var DragIcon = getIcon('drag-bar');
var SubMenu = /** @class */ (function (_super) {
    __extends(SubMenu, _super);
    function SubMenu() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * 内部使用的属性
         *
         * @memberof SubMenu
         */
        _this.internalProps = [
            'key',
            'style',
            'className',
            'title',
            'children',
            'disabled',
            'eventKey',
            'warnKey',
            'itemIcon',
            'expandIcon',
            'onMouseEnter',
            'onMouseLeave',
            'popupClassName',
            'popupOffset',
            'onClick',
            'onTitleClick',
            'onTitleMouseEnter',
            'onTitleMouseLeave'
        ];
        return _this;
    }
    SubMenu.prototype.handleSubmenuTitleActived = function (menuItemTitleInfo) {
        var key = menuItemTitleInfo.key, domEvent = menuItemTitleInfo.domEvent;
        var _a = this.context, onSubmenuClick = _a.onSubmenuClick, stacked = _a.stacked;
        var onTitleClick = this.props.onTitleClick;
        stacked && (onSubmenuClick === null || onSubmenuClick === void 0 ? void 0 : onSubmenuClick({ key: key, domEvent: domEvent, props: this.props }));
        onTitleClick === null || onTitleClick === void 0 ? void 0 : onTitleClick(__assign(__assign({}, menuItemTitleInfo), { keyPath: [key] }));
    };
    /** 检查icon参数值是否为文件路径 */
    SubMenu.prototype.isImgPath = function (raw) {
        return (typeof raw === 'string' &&
            (!!~raw.indexOf('.') || /^\/images\//.test(raw)));
    };
    SubMenu.prototype.renderSubMenuTitle = function () {
        var _a, _b, _c, _d;
        var _e, _f;
        var _g = this.context, collapsed = _g.collapsed, stacked = _g.stacked, mode = _g.mode, draggable = _g.draggable, onDragStart = _g.onDragStart;
        var _h = this.props, cx = _h.classnames, id = _h.id, label = _h.label, labelExtra = _h.labelExtra, icon = _h.icon, path = _h.path, depth = _h.depth, badge = _h.badge, badgeClassName = _h.badgeClassName, disabled = _h.disabled, defaultData = _h.data, extra = _h.extra, testIdBuilder = _h.testIdBuilder, renderLink = _h.renderLink;
        var isCollapsedNode = collapsed && depth === 1;
        var link = renderLink && typeof renderLink === 'function'
            ? renderLink(this.props)
            : path || '';
        var iconNode = icon ? (typeof icon === 'string' ? (this.isImgPath(icon) ? (React__default.createElement("div", { className: cx("Nav-Menu-item-icon") },
            React__default.createElement("img", { width: "14px", src: icon }))) : (React__default.createElement("i", { key: "icon", className: cx("Nav-Menu-item-icon", icon, (_a = {},
                _a['Nav-Menu-item-icon-collapsed'] = isCollapsedNode,
                _a)) }))) : React__default.isValidElement(icon) ? (React__default.cloneElement(icon, {
            className: cx("Nav-Menu-item-icon", (_e = icon.props) === null || _e === void 0 ? void 0 : _e.className, (_b = {},
                _b['Nav-Menu-item-icon-svg-collapsed'] = isCollapsedNode,
                _b))
        })) : null) : null;
        var labelNode = label && (typeof label === 'string' || Array.isArray(label)) ? (React__default.createElement("span", { className: cx('Nav-Menu-item-label', (_c = {},
                _c['Nav-Menu-item-label-collapsed'] = isCollapsedNode,
                _c['Nav-Menu-item-label-subTitle'] = !isCollapsedNode,
                _c)), title: isCollapsedNode || Array.isArray(label) ? '' : label }, isCollapsedNode ? label.slice(0, 1) : label)) : React__default.isValidElement(label) ? (React__default.cloneElement(label, {
            className: cx('Nav-Menu-item-label', (_f = label === null || label === void 0 ? void 0 : label.props) === null || _f === void 0 ? void 0 : _f.className, (_d = {},
                _d['Nav-Menu-item-label-collapsed'] = isCollapsedNode,
                _d['Nav-Menu-item-label-subTitle'] = !isCollapsedNode,
                _d))
        })) : null;
        var dragNode = !disabled && stacked && mode === 'inline' && !collapsed && draggable ? (React__default.createElement("span", __assign({ className: cx('Nav-Menu-item-dragBar'), draggable: true }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('drag-bar').getTestId()),
            React__default.createElement(DragIcon, null))) : null;
        var renderContent = function () {
            return isCollapsedNode ? (React__default.createElement(React__default.Fragment, null, iconNode || labelNode)) : (React__default.createElement(React__default.Fragment, null,
                dragNode,
                iconNode,
                labelNode,
                labelExtra,
                !stacked && depth === 1 ? (React__default.createElement("span", __assign({ key: "expand-toggle", className: cx('Nav-Menu-submenu-arrow') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('expand-toggle').getTestId()),
                    React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : null));
        };
        return (React__default.createElement("div", { className: cx('Nav-Menu-item-wrap') },
            React__default.createElement(Badge, { classnames: cx, badge: badge && !isCollapsedNode // 收起模式下 不展示角标
                    ? __assign(__assign({}, badge), { className: badgeClassName }) : null, data: createObject(defaultData, link) },
                React__default.createElement("a", __assign({ className: cx("Nav-Menu-item-link"), "data-id": (link === null || link === void 0 ? void 0 : link.__id) || id, "data-depth": depth, onDragStart: onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(link) }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId(), { href: stacked === false && (link === null || link === void 0 ? void 0 : link.to), onClick: function (e) {
                        var _a, _b;
                        e.preventDefault();
                        var clickEvent = new MouseEvent('click', {
                            bubbles: true,
                            cancelable: true
                        });
                        // 这里跳过a标签本身的跳转事件，让它继续传递让rc menu来处理
                        (_b = (_a = e.target.parentNode) === null || _a === void 0 ? void 0 : _a.dispatchEvent) === null || _b === void 0 ? void 0 : _b.call(_a, clickEvent);
                    } }), renderContent())),
            extra ? (React__default.createElement("div", { className: cx('Nav-Menu-item-extra') }, extra)) : null));
    };
    SubMenu.prototype.render = function () {
        var _a, _b;
        var _c = this.props, popupClassName = _c.popupClassName, cx = _c.classnames, hidden = _c.hidden, className = _c.className;
        var isDarkTheme = this.context.themeColor === 'dark';
        return hidden ? null : (React__default.createElement(SubMenu$2, __assign({}, pick(this.props, this.internalProps), { className: cx('Nav-Menu-submenu', (_a = {},
                _a['Nav-Menu-submenu-dark'] = isDarkTheme,
                _a), className), popupClassName: cx((_b = {},
                _b['Nav-Menu-submenu-popup-dark'] = isDarkTheme,
                _b), popupClassName), title: this.renderSubMenuTitle(), onTitleClick: this.handleSubmenuTitleActived })));
    };
    SubMenu.contextType = MenuContext;
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], SubMenu.prototype, "handleSubmenuTitleActived", null);
    return SubMenu;
}(React__default.Component));
var SubMenu$1 = themeable(SubMenu);

export { SubMenu, SubMenu$1 as default };
