export interface DocumentRecord {
  access_scope: "private" | "shared" | "public";
  id: string;
  workspace_id: string;
  title: string | null;
  parent_id: string | null;
  sort_order: number | null;
  is_starred: boolean | null;
  is_template: boolean;
  created_at: string;
  updated_at: string | null;
}

export interface DocumentNode extends DocumentRecord {
  children: DocumentNode[];
}

export function findBreadcrumb(records: DocumentRecord[], targetId: string): DocumentRecord[] {
  const map = new Map<string, DocumentRecord>();
  records.forEach((item) => map.set(item.id, item));
  const path: DocumentRecord[] = [];
  let current: DocumentRecord | undefined = map.get(targetId);

  while (current) {
    path.unshift(current);
    current = current.parent_id ? map.get(current.parent_id) : undefined;
  }

  return path;
}
