import { computed, defineComponent, h, inject, provide } from 'vue';
import NDropdownOption from './DropdownOption';
import NDropdownDivider from './DropdownDivider';
import NDropdownGroup from './DropdownGroup';
import { isSubmenuNode, isGroupNode, isDividerNode } from './utils';
import { dropdownInjectionKey } from './Dropdown';
export const dropdownMenuInjectionKey = Symbol('dropdownMenu');
export default defineComponent({
    name: 'DropdownMenu',
    props: {
        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 } = inject(dropdownInjectionKey);
        provide(dropdownMenuInjectionKey, {
            showIconRef: computed(() => {
                const renderIcon = renderIconRef.value;
                return props.tmNodes.some((tmNode) => {
                    const { rawNode } = tmNode;
                    if (isGroupNode(rawNode)) {
                        return rawNode.children.some((rawChild) => renderIcon ? renderIcon(rawChild) : rawChild.icon);
                    }
                    return renderIcon ? renderIcon(rawNode) : rawNode.icon;
                });
            }),
            hasSubmenuRef: computed(() => {
                return props.tmNodes.some((tmNode) => {
                    const { rawNode } = tmNode;
                    if (isGroupNode(rawNode)) {
                        return rawNode.children.some((rawChild) => isSubmenuNode(rawChild));
                    }
                    return isSubmenuNode(rawNode);
                });
            })
        });
    },
    render() {
        const { parentKey, clsPrefix } = this;
        return (h("div", { class: `${clsPrefix}-dropdown-menu` }, this.tmNodes.map((tmNode) => {
            if (isDividerNode(tmNode.rawNode)) {
                return h(NDropdownDivider, { clsPrefix: clsPrefix, key: tmNode.key });
            }
            if (isGroupNode(tmNode.rawNode)) {
                return (h(NDropdownGroup, { clsPrefix: clsPrefix, tmNode: tmNode, parentKey: parentKey, key: tmNode.key }));
            }
            return (h(NDropdownOption, { clsPrefix: clsPrefix, tmNode: tmNode, parentKey: parentKey, key: tmNode.key }));
        })));
    }
});
