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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var get = require('lodash/get');
var VueCompositionAPI = require('@vue/composition-api');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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__default["default"](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 DropdownMenuName = "TDropdownMenu";
var DropdownItemName = "TDropdownItem";
var getOptionsFromChildren = function getOptionsFromChildren(menuGroup) {
  if (!menuGroup || (menuGroup === null || menuGroup === void 0 ? void 0 : menuGroup.length) === 0) return [];
  if (get__default["default"](menuGroup, "Ctor.extendOptions.name") === DropdownMenuName) {
    var groupChildren = menuGroup.children;
    if (Array.isArray(groupChildren)) return getOptionsFromChildren(groupChildren);
    return [];
  }
  if (Array.isArray(menuGroup)) {
    return menuGroup.map(function (item) {
      var _item$componentOption, _groupChildren$filter, _groupChildren$filter2, _item$componentOption2, _item$data, _item$data2, _item$componentOption3, _item$componentOption4;
      var 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 {};
      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) {
        return ![DropdownMenuName, DropdownItemName].includes(get__default["default"](v, "componentOptions.Ctor.extendOptions.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) {
        return typeof v.text !== "string" && [DropdownMenuName, DropdownItemName].includes(get__default["default"](v, "componentOptions.Ctor.extendOptions.name"));
      });
      var commonProps = _objectSpread(_objectSpread({}, (_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: contentCtx || groupChildren
      });
      if (childrenCtx.length === 1) {
        return _objectSpread(_objectSpread({}, commonProps), {}, {
          children: childrenCtx.length > 0 ? getOptionsFromChildren(childrenCtx[0].componentOptions) : null
        });
      }
      return _objectSpread(_objectSpread({}, commonProps), {}, {
        children: childrenCtx.length > 0 ? getOptionsFromChildren(childrenCtx) : null
      });
    }).filter(function (v) {
      return !!v.content;
    });
  }
  return [];
};
function useDropdownOptions(props, slots) {
  var _slots$default, _slots$default$filter, _slots$default$filter2, _slots$dropdown, _slots$dropdown$;
  var 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(function (v) {
    return get__default["default"](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 VueCompositionAPI.computed(function () {
    if (props.options && props.options.length > 0) return props.options;
    return getOptionsFromChildren(menuSlot);
  });
}

exports["default"] = useDropdownOptions;
exports.getOptionsFromChildren = getOptionsFromChildren;
//# sourceMappingURL=useDropdownOptions.js.map
