import type { FolderTreeNode } from "@/lib/sidebar-nav-data"

/** Retourne les ids des parents à ouvrir pour afficher `targetId`, ou null. */
export function ancestorFolderIdsForTarget(
  nodes: FolderTreeNode[],
  targetId: string,
  chain: string[] = []
): string[] | null {
  for (const n of nodes) {
    if (n.id === targetId) return chain
    if (n.children?.length) {
      const found = ancestorFolderIdsForTarget(n.children, targetId, [
        ...chain,
        n.id,
      ])
      if (found) return found
    }
  }
  return null
}

export function folderSubtreeContainsId(
  node: FolderTreeNode,
  targetId: string
): boolean {
  if (node.id === targetId) return true
  return node.children?.some((c) => folderSubtreeContainsId(c, targetId)) ?? false
}
