Option Strict On
Option Explicit On

Imports System.Collections.Generic

Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASTreeLayoutEngine

        Friend Structure VisibleNodeEntry
            Friend Node As MASTreeNode
            Friend Depth As Integer
        End Structure

        Friend Function Flatten(rootNodes As IReadOnlyList(Of MASTreeNode)) As List(Of VisibleNodeEntry)
            Dim result As New List(Of VisibleNodeEntry)()

            If rootNodes Is Nothing Then Return result

            For Each n As MASTreeNode In rootNodes
                AppendVisible(result, n, 0)
            Next

            Return result
        End Function

        Private Sub AppendVisible(result As List(Of VisibleNodeEntry),
                                  node As MASTreeNode,
                                  depth As Integer)

            If result Is Nothing Then Return
            If node Is Nothing Then Return

            result.Add(New VisibleNodeEntry With {
                .Node = node,
                .Depth = depth
            })

            If node.IsExpanded AndAlso node.HasChildren Then
                For Each c As MASTreeNode In node.Children
                    AppendVisible(result, c, depth + 1)
                Next
            End If
        End Sub

    End Class

End Namespace