import { defineComponent, inject, shallowRef, onBeforeUnmount, createVNode } from 'vue';
import { Menu, Item } from '../../../components/menu/index.mjs';
import { Spin } from '../../../components/spin/index.mjs';
import MentionsContextKey from './MentionsContext.mjs';

function noop() {
}
var DropdownMenu = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "DropdownMenu",
  props: {
    prefixCls: String,
    options: {
      type: Array,
      default: () => []
    }
  },
  setup(props, {
    slots
  }) {
    const {
      activeIndex,
      setActiveIndex,
      selectOption,
      onFocus = noop,
      loading
    } = inject(MentionsContextKey, {
      activeIndex: shallowRef(),
      loading: shallowRef(false)
    });
    let timeoutId;
    const onMousedown = (e) => {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(() => {
        onFocus(e);
      });
    };
    onBeforeUnmount(() => {
      clearTimeout(timeoutId);
    });
    return () => {
      const {
        prefixCls,
        options
      } = props;
      const activeOption = options[activeIndex.value] || {};
      return createVNode(Menu, {
        "prefixCls": `${prefixCls}-menu`,
        "activeKey": activeOption.value,
        "onSelect": ({
          key
        }) => {
          const option = options.find(({
            value
          }) => value === key);
          selectOption(option);
        },
        "onMousedown": onMousedown
      }, {
        default: () => [!loading.value && options.map((option, index) => {
          const {
            value,
            disabled,
            label = option.value,
            class: className,
            style
          } = option;
          return createVNode(Item, {
            "key": value,
            "disabled": disabled,
            "onMouseenter": () => {
              setActiveIndex(index);
            },
            "class": className,
            "style": style
          }, {
            default: () => {
              var _a, _b;
              return [(_b = (_a = slots.option) == null ? void 0 : _a.call(slots, option)) != null ? _b : typeof label === "function" ? label(option) : label];
            }
          });
        }), !loading.value && options.length === 0 ? createVNode(Item, {
          "key": "notFoundContent",
          "disabled": true
        }, {
          default: () => {
            var _a;
            return [(_a = slots.notFoundContent) == null ? void 0 : _a.call(slots)];
          }
        }) : null, loading.value && createVNode(Item, {
          "key": "loading",
          "disabled": true
        }, {
          default: () => [createVNode(Spin, {
            "size": "small"
          }, null)]
        })]
      });
    };
  }
});

export { DropdownMenu as default };
//# sourceMappingURL=DropdownMenu.mjs.map
