{"version":3,"file":"tree-view-utils.mjs","names":[],"sources":["../../../src/tree-view/tree-view-utils.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport { type UniqueIdentifier } from '@dnd-kit/core';\nimport { arrayMove } from '@dnd-kit/sortable';\nimport { type RefObject } from 'react';\n\nimport { type FlattenedTreeItem, type TreeItem } from './tree-view-types';\n\nexport type SensorContext<T> = RefObject<{\n  items: FlattenedTreeItem<T>[];\n  offset: number;\n}>;\n\n/**\n * Takes a tree and flattens it into a list of item\n * where each item has a depth and a parentId.\n * @param tree - The tree to flatten\n * @param depth - The depth of the root of the tree (default 0)\n * @param parentId - The id of the parent of the root of the tree (default null)\n * @returns a list of items with depth and parentId\n */\nexport const flattenTree = <T>(\n  tree: TreeItem<T>[],\n  depth = 0,\n  parentId: UniqueIdentifier | null = null,\n): FlattenedTreeItem<T>[] => {\n  const flattened: FlattenedTreeItem<T>[] = [];\n\n  tree.forEach((item) => {\n    flattened.push({\n      ...item,\n      depth: depth,\n      parentId: parentId,\n      // Default values can go here for items\n      isSkeletonLoading: item.isSkeletonLoading ?? false,\n    });\n\n    if (item.canHaveSubItems) {\n      flattenTree(item.subItems, depth + 1, item.id).forEach((subItem) =>\n        flattened.push({ ...subItem }),\n      );\n    }\n  });\n\n  return flattened;\n};\n\n/**\n * Removes the children of the items with the given ids\n * @param items - The flattened tree items to remove the children from\n * @param ids - The ids of the items to remove the children from\n * @returns a new list of items with the children of the items with the given ids removed\n */\nexport function removeChildrenOf<T>(\n  items: FlattenedTreeItem<T>[],\n  ids: UniqueIdentifier[],\n) {\n  const excludeParentIds = [...ids];\n  return items.filter((item) => {\n    if (item.parentId !== null && excludeParentIds.includes(item.parentId)) {\n      if (item.canHaveSubItems && item.subItems.length > 0) {\n        excludeParentIds.push(item.id);\n      }\n      return false;\n    }\n    return true;\n  });\n}\n\n/**\n * Builds a tree from a list of flattened tree items\n * @param items - The flattened tree items\n * @returns the items as a tree data structure\n */\nexport function buildTree<T>(items: FlattenedTreeItem<T>[]): TreeItem<T>[] {\n  const tree: TreeItem<T>[] = [];\n\n  // Map array to object\n  const itemMap: { [id: string]: TreeItem<T> } = items.reduce(\n    (newItemMap, item) => {\n      const { id } = item;\n      // Add sub items array if it's allowed to have sub items.\n      const newItem = {\n        // Keep all item properties\n        ...item,\n        ...(item.canHaveSubItems && {\n          subItems: [],\n        }),\n      };\n      return {\n        ...newItemMap,\n        [id]: newItem,\n      };\n    },\n    {},\n  );\n\n  // Build the tree structure by assigning children to their parent items\n  items.forEach((item) => {\n    const { id, parentId } = item;\n    if (parentId !== null) {\n      const parentItem = itemMap[parentId];\n      if (parentItem && parentItem.canHaveSubItems) {\n        parentItem.subItems.push(itemMap[id]);\n      }\n    } else {\n      tree.push(itemMap[id]);\n    }\n  });\n\n  return tree;\n}\n\nfunction getDragDepth(offset: number, indentationWidth: number) {\n  return Math.round(offset / indentationWidth);\n}\n\nfunction getMaxDepth<T>({\n  previousItem,\n}: {\n  previousItem: FlattenedTreeItem<T>;\n}) {\n  if (previousItem && previousItem.canHaveSubItems === true) {\n    return previousItem.depth + 1;\n  }\n\n  return previousItem?.depth ?? 0;\n}\n\nfunction getMinDepth<T>({ nextItem }: { nextItem: FlattenedTreeItem<T> }) {\n  if (nextItem) {\n    return nextItem.depth;\n  }\n\n  return 0;\n}\n\n/**\n * Calculates the new depth, max depth, min depth and parent id of the item being dragged\n * Used to project the position of the dragged item. For example, to convert the item being dragged into a placeholder.\n * such as the line that appears when dragging an item in a tree view.\n * @param items - The flattened tree items\n * @param activeId - The id of the item being dragged\n * @param overId - The id of the item that the dragged item is being dragged over\n * @param dragOffset - The offset of the drag in pixels\n * @param indentationWidth - The width of the indentation in pixels\n * @returns the depth, max depth, min depth and parent id of the item being dragged\n */\nexport function getProjection<T>(\n  items: FlattenedTreeItem<T>[],\n  activeId: UniqueIdentifier,\n  overId: UniqueIdentifier,\n  dragOffset: number,\n  indentationWidth: number,\n) {\n  const overItemIndex = items.findIndex(({ id }) => id === overId);\n  const activeItemIndex = items.findIndex(({ id }) => id === activeId);\n  const activeItem = items[activeItemIndex];\n  const newItems = arrayMove(items, activeItemIndex, overItemIndex);\n  const previousItem = newItems[overItemIndex - 1];\n  const nextItem = newItems[overItemIndex + 1];\n  const dragDepth = getDragDepth(dragOffset, indentationWidth);\n  const projectedDepth = activeItem.depth + dragDepth;\n  const maxDepth = getMaxDepth({\n    previousItem,\n  });\n  const minDepth = getMinDepth({ nextItem });\n  let depth = projectedDepth;\n\n  if (projectedDepth >= maxDepth) {\n    depth = maxDepth;\n  } else if (projectedDepth < minDepth) {\n    depth = minDepth;\n  }\n\n  return { depth, maxDepth, minDepth, parentId: getParentId() };\n\n  function getParentId() {\n    if (depth === 0 || !previousItem) {\n      return null;\n    }\n\n    if (depth === previousItem.depth) {\n      return previousItem.parentId;\n    }\n\n    if (depth > previousItem.depth) {\n      return previousItem.id;\n    }\n\n    const newParent = newItems\n      .slice(0, overItemIndex)\n      .reverse()\n      .find((item) => item.depth === depth)?.parentId;\n\n    return newParent ?? null;\n  }\n}\n/**\n *\n * @param transform - A transform object\n * @returns a css transform string\n */\n\nexport const transformToString = (transform: {\n  scaleX: number;\n  scaleY: number;\n  // oxlint-disable-next-line id-length\n  x: number;\n  // oxlint-disable-next-line id-length\n  y: number;\n}) => {\n  return `scaleX(${transform.scaleX}) scaleY(${transform.scaleY}) translate3d(${transform.x}px, ${transform.y}px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