Option Strict On
Option Explicit On

Imports System
Imports System.Globalization
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned path policy for MASBreadcrumb. It owns segment hygiene, separator safety,
    ''' max-segment limits, selected-index clamping, overflow/collapse facts, RTL order facts,
    ''' and same-surface navigation boundaries without opening a router, history stack, menu,
    ''' popup, command executor, adapter, wrapper, or parallel navigation path.
    ''' </summary>
    Friend NotInheritable Class MASBreadcrumbPathPolicy
        Private Sub New()
        End Sub

        Friend Shared Function NormalizeItemText(value As String) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length > 72 Then text = text.Substring(0, 72)
            Return text
        End Function

        Friend Shared Function NormalizeSeparatorText(value As String) As String
            Dim normalized As String = If(value, String.Empty).Trim()
            If normalized.Length = 0 Then Return BreadcrumbTokens.SeparatorText
            If normalized.Length > 3 Then normalized = normalized.Substring(0, 3)
            Return normalized
        End Function

        Friend Shared Function NormalizeSelectedIndex(value As Integer,
                                                      itemCount As Integer) As Integer
            If itemCount <= 0 Then Return BreadcrumbTokens.DefaultSelectedIndex
            If value < 0 Then Return itemCount - 1
            If value >= itemCount Then Return itemCount - 1
            Return value
        End Function

        Friend Shared Function ResolveMoveIndex(currentIndex As Integer,
                                                delta As Integer,
                                                itemCount As Integer) As Integer
            If itemCount <= 0 Then Return BreadcrumbTokens.DefaultSelectedIndex
            Dim current As Integer = NormalizeSelectedIndex(currentIndex, itemCount)
            Dim nextIndex As Integer = current + delta
            If nextIndex < 0 Then nextIndex = 0
            If nextIndex >= itemCount Then nextIndex = itemCount - 1
            Return nextIndex
        End Function

        Friend Shared Function ResolveRightToLeft() As Boolean
            Return Nexamas.UI.Localization.MASLocalization.IsCurrentRightToLeft()
        End Function

        Friend Shared Function ResolveSeparatorForDirection(separatorText As String,
                                                            isRtl As Boolean) As String
            Dim normalized As String = NormalizeSeparatorText(separatorText)
            If isRtl AndAlso String.Equals(normalized, BreadcrumbTokens.SeparatorText, StringComparison.Ordinal) Then Return BreadcrumbTokens.RtlSeparatorText
            Return normalized
        End Function

        Friend Shared Function NormalizeSegmentCount(count As Integer) As Integer
            If count < 0 Then Return 0
            Return Math.Min(count, BreadcrumbTokens.MaxItems)
        End Function

        Friend Shared ReadOnly Property HasOverflowCollapsePolicy As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property HasNoRouterOrHistoryStack As Boolean
            Get
                Return True
            End Get
        End Property
    End Class

End Namespace
