// Helpers for rendering /api/projects rows as a flat indented list in
// `<select>` pickers (events filter, /deadlines/new, /appointments/new,
// Fristenrechner save modal, …). Backend `ProjectService.List` returns
// rows ordered by `path`, so a parent always precedes its descendants.
// Indenting by the path's dot-segment depth recreates the tree shape
// inside an HTML <option>.
//
// ` ` (NBSP) is used because some browsers collapse plain leading
// spaces inside <option> labels.

const INDENT_UNIT = "  ";

// projectDepth returns the 0-based depth of a project from its `path`
// (dot-separated UUIDs of the ancestor chain including self). Roots are
// depth 0; a child is depth 1; etc. A missing/empty path falls back to 0
// so we still render a usable label for legacy data.
export function projectDepth(path: string | null | undefined): number {
  if (!path) return 0;
  return path.split(".").length - 1;
}

// projectIndent returns the leading whitespace prefix for a project label
// at the given depth. Each level of nesting adds two NBSPs.
export function projectIndent(path: string | null | undefined): string {
  return INDENT_UNIT.repeat(projectDepth(path));
}
