import { ref, createVNode, shallowRef, watch } from 'vue';
import ASubMenu from '../SubMenu.mjs';
import AItemGroup from '../ItemGroup.mjs';
import ADivider from '../Divider.mjs';
import AMenuItem from '../MenuItem.mjs';

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: ref(parentKeys),
        parentKeys: ref(parentKeys),
        childrenEventKeys: ref(childrenEventKeys),
        isLeaf: false
      };
      if (children || type === "group") {
        if (type === "group") {
          const childrenNodes2 = convertItemsToNodes(children, store, parentMenuInfo);
          return createVNode(AItemGroup, {
            "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 createVNode(ASubMenu, {
          "key": mergedKey,
          ...restProps,
          "title": label,
          "originItemValue": opt
        }, {
          default: () => [childrenNodes]
        });
      }
      if (type === "divider")
        return createVNode(ADivider, {
          "key": mergedKey,
          ...restProps
        }, null);
      menuInfo.isLeaf = true;
      store.set(mergedKey, menuInfo);
      return createVNode(AMenuItem, {
        "key": mergedKey,
        ...restProps,
        "originItemValue": opt
      }, {
        default: () => [label]
      });
    }
    return null;
  }).filter((opt) => opt);
}
function useItems(props) {
  const itemsNodes = shallowRef([]);
  const hasItmes = shallowRef(false);
  const store = shallowRef(/* @__PURE__ */ new Map());
  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
  };
}

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