"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.dropdownMenuInjectionKey = void 0;
const vue_1 = require("vue");
const PopoverBody_1 = require("../../popover/src/PopoverBody");
const DropdownOption_1 = __importDefault(require("./DropdownOption"));
const DropdownDivider_1 = __importDefault(require("./DropdownDivider"));
const DropdownGroup_1 = __importDefault(require("./DropdownGroup"));
const DropdownRenderOption_1 = __importDefault(require("./DropdownRenderOption"));
const utils_1 = require("./utils");
const Dropdown_1 = require("./Dropdown");
exports.dropdownMenuInjectionKey = Symbol('dropdownMenu');
exports.default = (0, vue_1.defineComponent)({
    name: 'DropdownMenu',
    props: {
        showArrow: Boolean,
        arrowStyle: [String, Object],
        clsPrefix: {
            type: String,
            required: true
        },
        tmNodes: {
            type: Array,
            default: () => []
        },
        parentKey: {
            type: [String, Number],
            default: null
        }
    },
    setup(props) {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        const { renderIconRef, childrenFieldRef } = (0, vue_1.inject)(Dropdown_1.dropdownInjectionKey);
        (0, vue_1.provide)(exports.dropdownMenuInjectionKey, {
            showIconRef: (0, vue_1.computed)(() => {
                const renderIcon = renderIconRef.value;
                return props.tmNodes.some((tmNode) => {
                    var _a;
                    if (tmNode.isGroup) {
                        return (_a = tmNode.children) === null || _a === void 0 ? void 0 : _a.some(({ rawNode: rawChild }) => renderIcon ? renderIcon(rawChild) : rawChild.icon);
                    }
                    const { rawNode } = tmNode;
                    return renderIcon ? renderIcon(rawNode) : rawNode.icon;
                });
            }),
            hasSubmenuRef: (0, vue_1.computed)(() => {
                const { value: childrenField } = childrenFieldRef;
                return props.tmNodes.some((tmNode) => {
                    var _a;
                    if (tmNode.isGroup) {
                        return (_a = tmNode.children) === null || _a === void 0 ? void 0 : _a.some(({ rawNode: rawChild }) => (0, utils_1.isSubmenuNode)(rawChild, childrenField));
                    }
                    const { rawNode } = tmNode;
                    return (0, utils_1.isSubmenuNode)(rawNode, childrenField);
                });
            })
        });
    },
    render() {
        const { parentKey, clsPrefix } = this;
        return ((0, vue_1.h)("div", { class: `${clsPrefix}-dropdown-menu` },
            this.tmNodes.map((tmNode) => {
                if ((0, utils_1.isRenderNode)(tmNode.rawNode)) {
                    return ((0, vue_1.h)(DropdownRenderOption_1.default, { tmNode: tmNode, key: tmNode.key }));
                }
                if ((0, utils_1.isDividerNode)(tmNode.rawNode)) {
                    return (0, vue_1.h)(DropdownDivider_1.default, { clsPrefix: clsPrefix, key: tmNode.key });
                }
                if ((0, utils_1.isGroupNode)(tmNode.rawNode)) {
                    return ((0, vue_1.h)(DropdownGroup_1.default, { clsPrefix: clsPrefix, tmNode: tmNode, parentKey: parentKey, key: tmNode.key }));
                }
                return ((0, vue_1.h)(DropdownOption_1.default, { clsPrefix: clsPrefix, tmNode: tmNode, parentKey: parentKey, key: tmNode.key, props: tmNode.rawNode.props }));
            }),
            this.showArrow
                ? (0, PopoverBody_1.renderArrow)({
                    clsPrefix,
                    arrowStyle: this.arrowStyle
                })
                : null));
    }
});
