/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { getCurrentInstance, computed } from 'vue';
import isString from 'lodash/isString';
import isArray from 'lodash/isArray';
import camelCase from 'lodash/camelCase';
import { useChildComponentSlots } from '../../hooks/slot.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var getOptionsFromChildren = function getOptionsFromChildren(menuNode) {
  var _menuNode$, _menuNode$$type, _menuNode$3, _menuNode$4;
  if (!menuNode) return [];
  if (((_menuNode$ = menuNode[0]) === null || _menuNode$ === void 0 ? void 0 : (_menuNode$$type = _menuNode$.type) === null || _menuNode$$type === void 0 ? void 0 : _menuNode$$type.name) === "TDropdownMenu") {
    var _menuNode$2, _menuNode$2$children, _menuNode$2$children$;
    var groupChildren = (_menuNode$2 = menuNode[0]) === null || _menuNode$2 === void 0 ? void 0 : (_menuNode$2$children = _menuNode$2.children) === null || _menuNode$2$children === void 0 ? void 0 : (_menuNode$2$children$ = _menuNode$2$children["default"]) === null || _menuNode$2$children$ === void 0 ? void 0 : _menuNode$2$children$.call(_menuNode$2$children);
    if (isArray(groupChildren)) return getOptionsFromChildren(groupChildren);
  }
  if (isArray((_menuNode$3 = menuNode[0]) === null || _menuNode$3 === void 0 ? void 0 : _menuNode$3.children)) return getOptionsFromChildren((_menuNode$4 = menuNode[0]) === null || _menuNode$4 === void 0 ? void 0 : _menuNode$4.children);
  if (isArray(menuNode)) {
    menuNode = menuNode.reduce(function (acc, item) {
      acc = acc.concat(isArray(item.children) ? item.children : item);
      return acc;
    }, []);
    return menuNode.map(function (item) {
      var _item$children, _item$children$conten, _item$children2, _item$children2$prefi, _item$children3, _item$children3$prefi, _item$children4, _item$children4$defau, _groupChildren$filter, _groupChildren$filter2;
      var slotContent = (_item$children = item.children) === null || _item$children === void 0 ? void 0 : (_item$children$conten = _item$children.content) === null || _item$children$conten === void 0 ? void 0 : _item$children$conten.call(_item$children);
      var slotPrefixIcon = ((_item$children2 = item.children) === null || _item$children2 === void 0 ? void 0 : (_item$children2$prefi = _item$children2.prefixIcon) === null || _item$children2$prefi === void 0 ? void 0 : _item$children2$prefi.call(_item$children2)) || ((_item$children3 = item.children) === null || _item$children3 === void 0 ? void 0 : (_item$children3$prefi = _item$children3["prefix-icon"]) === null || _item$children3$prefi === void 0 ? void 0 : _item$children3$prefi.call(_item$children3));
      var groupChildren = (_item$children4 = item.children) === null || _item$children4 === void 0 ? void 0 : (_item$children4$defau = _item$children4["default"]) === null || _item$children4$defau === void 0 ? void 0 : _item$children4$defau.call(_item$children4);
      var contentCtx = groupChildren === null || groupChildren === void 0 ? void 0 : (_groupChildren$filter = groupChildren.filter) === null || _groupChildren$filter === void 0 ? void 0 : _groupChildren$filter.call(groupChildren, function (v) {
        var _v$type;
        return !["TDropdownMenu", "TDropdownItem"].includes((_v$type = v.type) === null || _v$type === void 0 ? void 0 : _v$type.name);
      });
      var childrenCtx = groupChildren === null || groupChildren === void 0 ? void 0 : (_groupChildren$filter2 = groupChildren.filter) === null || _groupChildren$filter2 === void 0 ? void 0 : _groupChildren$filter2.call(groupChildren, function (v) {
        var _v$type2;
        return !isString(v.children) && ["TDropdownMenu", "TDropdownItem"].includes((_v$type2 = v.type) === null || _v$type2 === void 0 ? void 0 : _v$type2.name);
      });
      var itemProps = Object.keys(item.props || {}).reduce(function (props, propName) {
        props[camelCase(propName)] = item.props[propName];
        return props;
      }, {});
      return _objectSpread(_objectSpread(_objectSpread({
        content: slotContent || contentCtx || groupChildren
      }, itemProps), slotPrefixIcon ? {
        prefixIcon: function prefixIcon() {
          return slotPrefixIcon;
        }
      } : {}), {}, {
        children: (childrenCtx === null || childrenCtx === void 0 ? void 0 : childrenCtx.length) > 0 ? getOptionsFromChildren(childrenCtx) : null
      });
    }).filter(function (v) {
      return !!v.content;
    });
  }
  return [];
};
function useDropdownOptions(props) {
  var _getChildComponentSlo, _getChildComponentSlo2, _getChildComponentSlo3, _getChildComponentSlo4, _instance$slots, _instance$slots$dropd;
  var getChildComponentSlots = useChildComponentSlots();
  var instance = getCurrentInstance();
  var menuSlot = ((_getChildComponentSlo = getChildComponentSlots("DropdownMenu")) === null || _getChildComponentSlo === void 0 ? void 0 : (_getChildComponentSlo2 = _getChildComponentSlo[0]) === null || _getChildComponentSlo2 === void 0 ? void 0 : (_getChildComponentSlo3 = _getChildComponentSlo2.children) === null || _getChildComponentSlo3 === void 0 ? void 0 : (_getChildComponentSlo4 = _getChildComponentSlo3["default"]) === null || _getChildComponentSlo4 === void 0 ? void 0 : _getChildComponentSlo4.call(_getChildComponentSlo3)) || ((_instance$slots = instance.slots) === null || _instance$slots === void 0 ? void 0 : (_instance$slots$dropd = _instance$slots.dropdown) === null || _instance$slots$dropd === void 0 ? void 0 : _instance$slots$dropd.call(_instance$slots));
  var dropdownOptions = computed(function () {
    if (props.options && props.options.length > 0) return props.options;
    return getOptionsFromChildren(menuSlot);
  });
  return dropdownOptions;
}

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