Option Strict On
Option Explicit On

Imports System
Imports System.Text
Imports System.Windows.Forms

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only policy for the SearchTextBox gateway. The control keeps the
    ''' official MASInputBoxSkiaBase edit surface and the existing SearchBoxSystem
    ''' state. This policy owns search-specific text hygiene, keyboard intent,
    ''' suggestion-popup limits, and official float owner naming only.
    ''' </summary>
    Friend NotInheritable Class MASSearchTextBoxPolicy

        Private Const DefaultSuggestionLimit As Integer = 8
        Private Const MaximumSuggestionLimit As Integer = 20
        Private Const DefaultSuggestionOwnerKey As String = "MAS.SearchTextBox.AdvancedSuggestions"

        Private Sub New()
        End Sub

        Friend Shared Function NormalizeSearchText(value As String) As String
            Dim s As String = If(value, String.Empty)
            s = s.Replace(vbCrLf, " ")
            s = s.Replace(vbCr, " ")
            s = s.Replace(vbLf, " ")
            s = s.Replace(vbTab, " ")
            Return CollapseSpaces(RemoveUnsupportedControlCharacters(s))
        End Function

        Friend Shared Function NormalizePlaceholder(value As String) As String
            Return NormalizeSearchText(value).Trim()
        End Function

        Friend Shared Function NormalizeMaxLength(value As Integer) As Integer
            If value < 0 Then Return 0
            Return value
        End Function

        Friend Shared Function ResolveDefaultPlaceholder() As String
            Return "Search..."
        End Function

        Friend Shared Function ShouldCommitFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Enter
        End Function

        Friend Shared Function ShouldCloseSuggestionsFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Escape
        End Function

        Friend Shared Function ShouldOpenSuggestionsFromKey(keyCode As Keys) As Boolean
            Return keyCode = Keys.Down OrElse keyCode = Keys.F4
        End Function

        Friend Shared Function NormalizeSuggestionPopupLimit(value As Integer) As Integer
            If value <= 0 Then Return DefaultSuggestionLimit
            Return Math.Max(1, Math.Min(MaximumSuggestionLimit, value))
        End Function

        Friend Shared Function ResolveVisibleSuggestionRows(itemCount As Integer) As Integer
            If itemCount <= 0 Then Return 0
            Return Math.Max(1, Math.Min(6, itemCount))
        End Function

        Friend Shared Function ResolveSuggestionOwnerKey(ownerKey As String) As String
            If String.IsNullOrWhiteSpace(ownerKey) Then Return DefaultSuggestionOwnerKey
            Return ownerKey.Trim()
        End Function

        Friend Shared Function HasOfficialSuggestionFloatRoute() As Boolean
            Return True
        End Function

        Friend Shared Function HasNoParallelSearchPath() As Boolean
            Return True
        End Function

        Friend Shared Function HasNoNativeTextBoxRoute() As Boolean
            Return True
        End Function

        Private Shared Function RemoveUnsupportedControlCharacters(value As String) As String
            If String.IsNullOrEmpty(value) Then Return String.Empty

            Dim result As New StringBuilder(value.Length)
            For Each ch As Char In value
                If Char.IsControl(ch) Then Continue For
                result.Append(ch)
            Next

            Return result.ToString()
        End Function

        Private Shared Function CollapseSpaces(value As String) As String
            If String.IsNullOrEmpty(value) Then Return String.Empty

            Dim result As New StringBuilder(value.Length)
            Dim previousWasSpace As Boolean
            For Each ch As Char In value
                Dim isSpace As Boolean = Char.IsWhiteSpace(ch)
                If isSpace Then
                    If Not previousWasSpace Then result.Append(" "c)
                    previousWasSpace = True
                Else
                    result.Append(ch)
                    previousWasSpace = False
                End If
            Next

            Return result.ToString()
        End Function

    End Class

End Namespace
