'use strict';

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

var vue = require('vue');
var SubMenu = require('../SubMenu.cjs');
var ItemGroup = require('../ItemGroup.cjs');
var Divider = require('../Divider.cjs');
var MenuItem = require('../MenuItem.cjs');

function convertItemsToNodes(list, store, parentMenuInfo) {
  return (list || []).map((opt, index) => {
    if (opt && typeof opt === "object") {
      const {
        label,
        children,
        key,
        type,
        ...restProps
      } = opt;
      const mergedKey = key != null ? key : `tmp-${index}`;
      const parentKeys = parentMenuInfo ? parentMenuInfo.parentKeys.slice() : [];
      const childrenEventKeys = [];
      const menuInfo = {
        eventKey: mergedKey,
        key: mergedKey,
        parentEventKeys: vue.ref(parentKeys),
        parentKeys: vue.ref(parentKeys),
        childrenEventKeys: vue.ref(childrenEventKeys),
        isLeaf: false
      };
      if (children || type === "group") {
        if (type === "group") {
          const childrenNodes2 = convertItemsToNodes(children, store, parentMenuInfo);
          return vue.createVNode(ItemGroup.default, {
            "key": mergedKey,
            ...restProps,
            "title": label,
            "originItemValue": opt
          }, {
            default: () => [childrenNodes2]
          });
        }
        store.set(mergedKey, menuInfo);
        if (parentMenuInfo)
          parentMenuInfo.childrenEventKeys.push(mergedKey);
        const childrenNodes = convertItemsToNodes(children, store, {
          childrenEventKeys,
          parentKeys: [].concat(parentKeys, mergedKey)
        });
        return vue.createVNode(SubMenu.default, {
          "key": mergedKey,
          ...restProps,
          "title": label,
          "originItemValue": opt
        }, {
          default: () => [childrenNodes]
        });
      }
      if (type === "divider")
        return vue.createVNode(Divider.default, {
          "key": mergedKey,
          ...restProps
        }, null);
      menuInfo.isLeaf = true;
      store.set(mergedKey, menuInfo);
      return vue.createVNode(MenuItem.default, {
        "key": mergedKey,
        ...restProps,
        "originItemValue": opt
      }, {
        default: () => [label]
      });
    }
    return null;
  }).filter((opt) => opt);
}
function useItems(props) {
  const itemsNodes = vue.shallowRef([]);
  const hasItmes = vue.shallowRef(false);
  const store = vue.shallowRef(/* @__PURE__ */ new Map());
  vue.watch(() => props.items, () => {
    const newStore = /* @__PURE__ */ new Map();
    hasItmes.value = false;
    if (props.items) {
      hasItmes.value = true;
      itemsNodes.value = convertItemsToNodes(props.items, newStore);
    } else {
      itemsNodes.value = void 0;
    }
    store.value = newStore;
  }, {
    immediate: true,
    deep: true
  });
  return {
    itemsNodes,
    store,
    hasItmes
  };
}

exports.default = useItems;
//# sourceMappingURL=useItems.cjs.map
