Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only TreeGrid source descriptor. It lets internal data-source adapters feed
    ''' stable hierarchical rows into MASTreeGrid without exposing a public binding engine
    ''' or depending on WinForms TreeView/DataGrid wrappers.
    ''' </summary>
    Friend NotInheritable Class MASTreeGridSourceRow

        Private ReadOnly _values As IReadOnlyList(Of String)

        Friend Sub New(key As String,
                       label As String,
                       Optional values As IEnumerable(Of String) = Nothing,
                       Optional parentKey As String = "",
                       Optional expanded As Boolean = True)
            Me.Key = If(key, String.Empty).Trim()
            Me.ParentKey = If(parentKey, String.Empty).Trim()
            Me.Label = MASTreeGridPolicy.NormalizeRowLabel(label)
            Me.Expanded = expanded

            Dim normalized As New List(Of String)()
            If values IsNot Nothing Then
                For Each value As String In values
                    normalized.Add(MASTreeGridPolicy.NormalizeCellText(value))
                Next
            End If
            _values = New ReadOnlyCollection(Of String)(normalized.ToArray())
        End Sub

        Friend ReadOnly Property Key As String
        Friend ReadOnly Property ParentKey As String
        Friend ReadOnly Property Label As String
        Friend ReadOnly Property Expanded As Boolean

        Friend ReadOnly Property Values As IReadOnlyList(Of String)
            Get
                Return _values
            End Get
        End Property

        Friend Shared Function FromItem(stableKey As String,
                                        item As Object) As MASTreeGridSourceRow
            Dim existing As MASTreeGridSourceRow = TryCast(item, MASTreeGridSourceRow)
            If existing IsNot Nothing Then Return existing
            Dim key As String = If(stableKey, String.Empty).Trim()
            Dim label As String = If(item IsNot Nothing, item.ToString(), String.Empty)
            Return New MASTreeGridSourceRow(key, label, Nothing, String.Empty, True)
        End Function

    End Class

End Namespace
