{"version":3,"file":"ItemNode.cjs","sources":["../../../../src/components/Browser/models/ItemNode.ts"],"sourcesContent":["import { NodeContext } from './NodeContext'\n\nimport type { Node } from './Node'\n\nconst NODE_MAX_DEPTH = 3\n\nexport type ItemNodeLike = {\n  value: string\n  label: string\n  children?: ItemNodeLike[]\n}\n\nexport class ItemNode {\n  readonly value: string\n  readonly label: string\n  readonly context: NodeContext<ItemNode, ItemNode>\n\n  constructor(value: string, label: string, children: ItemNode[] = []) {\n    this.value = value\n    this.label = label\n    this.context = new NodeContext(this)\n\n    for (const child of children) {\n      this.append(child)\n    }\n  }\n\n  static from(item: ItemNodeLike): ItemNode {\n    return new ItemNode(\n      item.value,\n      item.label,\n      (item.children ?? []).map((itemNodeLike) => ItemNode.from(itemNodeLike)),\n    )\n  }\n\n  get parent(): Node | undefined {\n    return this.context.parent?.node\n  }\n\n  get children(): ItemNode[] {\n    return this.context.children.map((child) => child.node)\n  }\n\n  append(that: ItemNode): void {\n    if (this.#getDepth() >= NODE_MAX_DEPTH) {\n      throw new Error(`Cannot append to a node deeper than ${NODE_MAX_DEPTH}`)\n    }\n\n    this.context.append(that.context)\n  }\n\n  getNext(): ItemNode | undefined {\n    if (!this.parent) {\n      return\n    }\n\n    const index = this.#getIndex()\n\n    if (index === -1) {\n      return\n    }\n\n    return this.parent.children[index + 1]\n  }\n\n  getPrev(): ItemNode | undefined {\n    if (!this.parent) {\n      return\n    }\n\n    const index = this.#getIndex()\n\n    if (index === -1) {\n      return\n    }\n\n    return this.parent.children[index - 1]\n  }\n\n  getFirstChild(): ItemNode | undefined {\n    return this.children.at(0)\n  }\n\n  getLastChild(): ItemNode | undefined {\n    return this.children.at(-1)\n  }\n\n  getAncestors(): ItemNode[] {\n    const ancestors: ItemNode[] = []\n\n    let current: Node | undefined = this.parent\n\n    while (current instanceof ItemNode) {\n      ancestors.unshift(current)\n      current = current.parent\n    }\n\n    return ancestors\n  }\n\n  getSiblings(): ItemNode[] {\n    if (!this.parent) {\n      return []\n    }\n\n    return this.parent.children\n  }\n\n  findByValue(value: string): ItemNode | undefined {\n    if (this.value === value) {\n      return this\n    }\n\n    for (const child of this.children) {\n      const found = child.findByValue(value)\n\n      if (found) {\n        return found\n      }\n    }\n    return\n  }\n\n  #getIndex(): number {\n    if (!this.parent) {\n      return -1\n    }\n\n    return this.parent.children.indexOf(this)\n  }\n\n  #getDepth(): number {\n    return this.getAncestors().length + 1\n  }\n}\n"],"names":["NodeContext"],"mappings":";;;;AAIA,MAAM,cAAc,GAAG,CAAC;MAQX,QAAQ,CAAA;AACV,IAAA,KAAK;AACL,IAAA,KAAK;AACL,IAAA,OAAO;AAEhB,IAAA,WAAA,CAAY,KAAa,EAAE,KAAa,EAAE,WAAuB,EAAE,EAAA;AACjE,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;QAClB,IAAI,CAAC,OAAO,GAAG,IAAIA,iDAAW,CAAC,IAAI,CAAC;AAEpC,QAAA,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE;AAC5B,YAAA,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;QACpB;IACF;IAEA,OAAO,IAAI,CAAC,IAAkB,EAAA;AAC5B,QAAA,OAAO,IAAI,QAAQ,CACjB,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,KAAK,EACV,CAAC,IAAI,CAAC,QAAQ,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC,YAAY,KAAK,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CACzE;IACH;AAEA,IAAA,IAAI,MAAM,GAAA;AACR,QAAA,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI;IAClC;AAEA,IAAA,IAAI,QAAQ,GAAA;AACV,QAAA,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,IAAI,CAAC;IACzD;AAEA,IAAA,MAAM,CAAC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