using System.Collections.Generic; namespace Spectre.Console { internal sealed class ListPromptItem : IMultiSelectionItem where T : notnull { public T Data { get; } public ListPromptItem? Parent { get; } public List> Children { get; } public int Depth { get; } public bool IsSelected { get; set; } public bool IsGroup => Children.Count > 0; public ListPromptItem(T data, ListPromptItem? parent = null) { Data = data; Parent = parent; Children = new List>(); Depth = CalculateDepth(parent); } public IMultiSelectionItem Select() { IsSelected = true; return this; } public ISelectionItem AddChild(T item) { var node = new ListPromptItem(item, this); Children.Add(node); return node; } public IEnumerable> Traverse(bool includeSelf) { var stack = new Stack>(); if (includeSelf) { stack.Push(this); } else { foreach (var child in Children) { stack.Push(child); } } while (stack.Count > 0) { var current = stack.Pop(); yield return current; if (current.Children.Count > 0) { foreach (var child in current.Children.ReverseEnumerable()) { stack.Push(child); } } } } private static int CalculateDepth(ListPromptItem? parent) { var level = 0; while (parent != null) { level++; parent = parent.Parent; } return level; } } }