"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const types = require("../../../utils/types.js");
const node = require("./node.js");
const isEqual = require("../../../../../lodash-es/isEqual.js");
const flatNodes = (nodes, leafOnly) => {
  return nodes.reduce((res, node2) => {
    if (node2.isLeaf) res.push(node2);
    else {
      !leafOnly && res.push(node2);
      res = res.concat(flatNodes(node2.children, leafOnly));
    }
    return res;
  }, []);
};
var Store = class {
  constructor(data, config) {
    this.config = config;
    const nodes = (data || []).map((nodeData) => new node.default(nodeData, this.config));
    this.nodes = nodes;
    this.allNodes = flatNodes(nodes, false);
    this.leafNodes = flatNodes(nodes, true);
  }
  getNodes() {
    return this.nodes;
  }
  getFlattedNodes(leafOnly) {
    return leafOnly ? this.leafNodes : this.allNodes;
  }
  appendNode(nodeData, parentNode) {
    const node$1 = parentNode ? parentNode.appendChild(nodeData) : new node.default(nodeData, this.config);
    if (!parentNode) this.nodes.push(node$1);
    this.appendAllNodesAndLeafNodes(node$1);
  }
  appendNodes(nodeDataList, parentNode) {
    if (nodeDataList.length > 0) nodeDataList.forEach((nodeData) => this.appendNode(nodeData, parentNode));
    else parentNode && parentNode.isLeaf && this.leafNodes.push(parentNode);
  }
  appendAllNodesAndLeafNodes(node2) {
    this.allNodes.push(node2);
    node2.isLeaf && this.leafNodes.push(node2);
    if (node2.children) node2.children.forEach((subNode) => {
      this.appendAllNodesAndLeafNodes(subNode);
    });
  }
  getNodeByValue(value, leafOnly = false) {
    if (types.isPropAbsent(value)) return null;
    return this.getFlattedNodes(leafOnly).find((node2) => isEqual.default(node2.value, value) || isEqual.default(node2.pathValues, value)) || null;
  }
  getSameNode(node2) {
    if (!node2) return null;
    return this.getFlattedNodes(false).find(({ value, level }) => isEqual.default(node2.value, value) && node2.level === level) || null;
  }
};
exports.default = Store;
