/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import get from 'lodash/get';
import { computed } from '@vue/composition-api';

const DropdownMenuName = "LDropdownMenu";
const DropdownItemName = "LDropdownItem";
const getOptionsFromChildren = menuGroup => {
  if (!menuGroup || (menuGroup === null || menuGroup === void 0 ? void 0 : menuGroup.length) === 0) return [];
  if (get(menuGroup, "Ctor.extendOptions.name") === DropdownMenuName) {
    const groupChildren = menuGroup.children;
    if (Array.isArray(groupChildren)) return getOptionsFromChildren(groupChildren);
    return [];
  }
  if (Array.isArray(menuGroup)) {
    return menuGroup.map(item => {
      var _item$componentOption, _groupChildren$findIn, _groupChildren$filter, _item$componentOption2, _item$data, _item$data2, _item$componentOption3, _item$componentOption4, _groupChildren$conten;
      const groupChildren = item === null || item === void 0 ? void 0 : (_item$componentOption = item.componentOptions) === null || _item$componentOption === void 0 ? void 0 : _item$componentOption.children;
      if (!groupChildren) return {};
      const contentIdx = (_groupChildren$findIn = groupChildren.findIndex) === null || _groupChildren$findIn === void 0 ? void 0 : _groupChildren$findIn.call(groupChildren, v => typeof v.text === "string");
      const childrenContent = (_groupChildren$filter = groupChildren.filter) === null || _groupChildren$filter === void 0 ? void 0 : _groupChildren$filter.call(groupChildren, v => typeof v.text !== "string" && [DropdownMenuName, DropdownItemName].includes(get(v, "componentOptions.Ctor.extendOptions.name")));
      const commonProps = {
        ...((_item$componentOption2 = item.componentOptions) === null || _item$componentOption2 === void 0 ? void 0 : _item$componentOption2.propsData),
        style: item === null || item === void 0 ? void 0 : (_item$data = item.data) === null || _item$data === void 0 ? void 0 : _item$data.style,
        class: item === null || item === void 0 ? void 0 : (_item$data2 = item.data) === null || _item$data2 === void 0 ? void 0 : _item$data2.staticClass,
        onClick: (_item$componentOption3 = item.componentOptions) === null || _item$componentOption3 === void 0 ? void 0 : (_item$componentOption4 = _item$componentOption3.listeners) === null || _item$componentOption4 === void 0 ? void 0 : _item$componentOption4.click,
        content: ((_groupChildren$conten = groupChildren[contentIdx]) === null || _groupChildren$conten === void 0 ? void 0 : _groupChildren$conten.text) || groupChildren
      };
      if (childrenContent.length === 1) {
        return {
          ...commonProps,
          children: childrenContent.length > 0 ? getOptionsFromChildren(childrenContent[0].componentOptions) : null
        };
      }
      return {
        ...commonProps,
        children: childrenContent.length > 0 ? getOptionsFromChildren(childrenContent) : null
      };
    }).filter(v => !!v.content);
  }
  return [];
};
function useDropdownOptions(props, slots) {
  var _slots$default, _slots$default$filter, _slots$default$filter2, _slots$dropdown, _slots$dropdown$;
  const menuSlot = (slots === null || slots === void 0 ? void 0 : (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : (_slots$default$filter = _slots$default.filter(v => get(v, "componentOptions.Ctor.extendOptions.name") === DropdownMenuName)) === null || _slots$default$filter === void 0 ? void 0 : (_slots$default$filter2 = _slots$default$filter[0]) === null || _slots$default$filter2 === void 0 ? void 0 : _slots$default$filter2.componentOptions) || (slots === null || slots === void 0 ? void 0 : (_slots$dropdown = slots.dropdown) === null || _slots$dropdown === void 0 ? void 0 : (_slots$dropdown$ = _slots$dropdown[0]) === null || _slots$dropdown$ === void 0 ? void 0 : _slots$dropdown$.componentOptions);
  return computed(() => {
    if (props.options && props.options.length > 0) return props.options;
    return getOptionsFromChildren(menuSlot);
  });
}

export { useDropdownOptions as default, getOptionsFromChildren };
//# sourceMappingURL=use-dropdown-options.js.map
