import { Tree } from 'src/types/breadcrumbs';

export const getBreadcrumbs = <Leaf>(leafTree: Tree<Leaf>): Leaf[] => {
  if (!leafTree) return [];

  let { children, ...leaf } = leafTree;

  const leafs: Leaf[] = [leaf as Leaf];

  while (children && children.length > 0) {
    [{ children, ...leaf }] = children;
    leafs.push(leaf as Leaf);
  }

  return leafs;
};
