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

import _defineProperty from '@babel/runtime/helpers/defineProperty';

const getTreePaths = (node, val, ans) => {
  if (!node) return;
  for (let i = 0; i < node.children.length; ++i) {
    const child = node.children[i];
    if (child.value === val) return [...ans, node.value];
    const target = getTreePaths(child, val, [...ans, node.value]);
    if (target) return target;
  }
};
const getTreeSameParentNodes = (node, val) => {
  if (!node) return;
  for (let i = 0; i < node.children.length; ++i) {
    const child = node.children[i];
    if (child.value === val) return node.children;
    const target = getTreeSameParentNodes(child, val);
    if (target) return target;
  }
};
const DFS = (root, val) => {
  if (root.value === val) return root;
  if (root.children.length > 0) {
    for (let i = 0, len = root.children.length; i < len; i++) {
      const res = DFS(root.children[i], val);
      if (res) return res;
    }
  }
};
class VMenu {
  constructor(options) {
    _defineProperty(this, "data", null);
    _defineProperty(this, "cache", /* @__PURE__ */new Set());
    _defineProperty(this, "isMutex", false);
    _defineProperty(this, "expandValues", null);
    const root = {
      value: null,
      parent: null,
      children: []
    };
    this.data = root;
    this.isMutex = options === null || options === void 0 ? void 0 : options.isMutex;
    this.expandValues = new Set(options === null || options === void 0 ? void 0 : options.expandValues);
  }
  add(item) {
    const {
      value,
      parent,
      vnode
    } = item;
    const node = {
      value,
      parent,
      vnode,
      children: []
    };
    this.cache.forEach((data, v2, set) => {
      if (item.value === data.parent) {
        node.children.push(data);
        set.delete(data);
      }
    });
    if (item.parent == null) {
      this.data.children.push(node);
      node.parent = this.data;
    } else if (this.data.children.length > 0) {
      const pNode = DFS(this.data, parent);
      if (pNode) pNode.children.push(node);else this.cache.add(node);
    } else {
      this.cache.add(node);
    }
  }
  select(val) {
    const activeValues = getTreePaths(this.data, val, []) || [];
    activeValues.push(val);
    return activeValues.filter(val2 => val2 != null);
  }
  expand(val) {
    if (this.expandValues.has(val)) {
      this.expandValues.delete(val);
      return [...this.expandValues];
    }
    this.expandValues.add(val);
    if (!this.isMutex) return [...this.expandValues];
    const sameParentNodes = getTreeSameParentNodes(this.data, val);
    const sameLevelSubmenuValues = new Set(sameParentNodes.filter(node => {
      var _node$children;
      return ((_node$children = node.children) === null || _node$children === void 0 ? void 0 : _node$children.length) > 0 && node.value !== val;
    }).map(child => child.value));
    this.expandValues.forEach(val2 => {
      const isHit = sameLevelSubmenuValues.has(val2);
      if (isHit) this.expandValues.delete(val2);
    });
    return [...this.expandValues];
  }
  getChild(value) {
    const target = DFS(this.data, value);
    return target ? target.children : [];
  }
}

export { VMenu as default };
//# sourceMappingURL=v-menu.js.map
