import { CatalogueNodeType } from '@penx/model-type' import type { CatalogueNode } from './CatalogueNode' import { WithFlattenProps } from './types' export function flattenTree( items: CatalogueNode[], type?: CatalogueNodeType, parentId: string | null = null, depth = 0, ): WithFlattenProps[] { return items.reduce[]>((acc, item, index) => { const flattenNode = Object.assign(item, { parentId, depth, index }) if (typeof type === 'undefined') acc.push(flattenNode) if (type === item.type) acc.push(flattenNode) if (item?.children?.length) { acc.push(...flattenTree(item.children || [], type, item.id, depth + 1)) } return acc }, []) }