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

import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';

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

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