/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var pick = require('lodash/pick');
var RcMenu = require('rc-menu');
var amisCore = require('amis-core');
var icons = require('../icons.js');
var Badge = require('../Badge.js');
var MenuContext = require('./MenuContext.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var DragIcon = icons.getIcon('drag-bar');
var SubMenu = /** @class */ (function (_super) {
    tslib.__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 (_a) {
        var key = _a.key, domEvent = _a.domEvent;
        var _b = this.context, onSubmenuClick = _b.onSubmenuClick, stacked = _b.stacked;
        stacked && (onSubmenuClick === null || onSubmenuClick === void 0 ? void 0 : onSubmenuClick({ key: key, domEvent: domEvent, props: this.props }));
    };
    /** 检查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) ? (_J$X_("div", { className: cx("Nav-Menu-item-icon") },
            _J$X_("img", { width: "14px", src: icon }))) : (_J$X_("i", { key: "icon", className: cx("Nav-Menu-item-icon", icon, (_a = {},
                _a['Nav-Menu-item-icon-collapsed'] = isCollapsedNode,
                _a)) }))) : React__default["default"].isValidElement(icon) ? (React__default["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)) ? (_J$X_("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["default"].isValidElement(label) ? (React__default["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 ? (_J$X_("span", tslib.__assign({ className: cx('Nav-Menu-item-dragBar'), draggable: true }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('drag-bar').getTestId()),
            _J$X_(DragIcon, null))) : null;
        var renderContent = function () {
            return isCollapsedNode ? (_J$X_(React__default["default"].Fragment, null, iconNode || labelNode)) : (_J$X_(React__default["default"].Fragment, null,
                dragNode,
                iconNode,
                labelNode,
                labelExtra,
                !stacked && depth === 1 ? (_J$X_("span", tslib.__assign({ key: "expand-toggle", className: cx('Nav-Menu-submenu-arrow') }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('expand-toggle').getTestId()),
                    _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))) : null));
        };
        return (_J$X_("div", { className: cx('Nav-Menu-item-wrap') },
            _J$X_(Badge.Badge, { classnames: cx, badge: badge && !isCollapsedNode // 收起模式下 不展示角标
                    ? tslib.__assign(tslib.__assign({}, badge), { className: badgeClassName }) : null, data: amisCore.createObject(defaultData, link) },
                _J$X_("a", tslib.__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) }), renderContent())),
            extra ? (_J$X_("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 : (_J$X_(RcMenu.SubMenu, tslib.__assign({}, pick__default["default"](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.MenuContext;
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], SubMenu.prototype, "handleSubmenuTitleActived", null);
    return SubMenu;
}(React__default["default"].Component));
var SubMenu$1 = amisCore.themeable(SubMenu);

exports.SubMenu = SubMenu;
exports["default"] = SubMenu$1;
