namespace VoiceCat.Windows;
/// A channel and its ordered children as displayed by the Windows client.
public sealed record ChannelHierarchyItem(
ChannelInfo Channel,
IReadOnlyList Children);
public static class ChannelHierarchy
{
///
/// Builds the server-defined channel hierarchy. SortOrder orders siblings; LINQ's stable
/// ordering preserves the server's sequence when siblings have the same value.
///
public static IReadOnlyList Build(IReadOnlyList channels)
{
var byParent = channels
.GroupBy(channel => channel.ParentId)
.ToDictionary(group => group.Key, group => group.OrderBy(channel => channel.SortOrder).ToArray());
IReadOnlyList AddChildren(uint parentId)
{
if (!byParent.TryGetValue(parentId, out ChannelInfo[]? children))
return [];
return children
.Select(channel => new ChannelHierarchyItem(channel, AddChildren(channel.Id)))
.ToArray();
}
return AddChildren(0);
}
}