class Node {
  #count

  constructor(data, isComplete = false) {
    this.data = data;
    this.children = {};
    this.isComplete = isComplete;
    this.#count = 0;
  }

  get count() {
    return this.#count;
  }

  incrementCount() {
    this.#count += 1;
  }
}

class Trie {
  constructor(words = []) {
    this.root = new Node();
    this.populate(words);
  }

  populate(words) {
    for (const word of words) {
      this.add(word);
    }
  }

  add(parts, node = this.root) {
    if (typeof parts === "string") {
      parts = parts.split("_");
    }

    node.incrementCount();

    if (parts.length === 0) {
      node.isComplete = true;
      return;
    }

    const part = parts.shift();

    if (node.children[part]) {
      this.add(parts, node.children[part]);
    } else {
      const newNode = new Node(part);
      node.children[part] = newNode;
      this.add(parts, newNode);
    }
  }

  getRoot() {
    return this.root;
  }
}

export {
  Trie,
};
