Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-owned normalization and semantic policy for MASFormBuilder/MASDataForm field rows.
    ''' The policy keeps the public builder small and fluent while preventing label/value/helper,
    ''' required, state, and field-kind rules from being duplicated inside the builder and the
    ''' visual DataForm control.
    ''' </summary>
    Friend NotInheritable Class MASFormBuilderFieldPolicy
        Private Sub New()
        End Sub

        Friend Const MaxLabelLength As Integer = 80
        Friend Const MaxValueLength As Integer = 160
        Friend Const MaxHelperLength As Integer = 180

        Friend Shared Function NormalizeLabel(label As String) As String
            Return NormalizeText(label, "Field", MaxLabelLength)
        End Function

        Friend Shared Function NormalizeValueText(valueText As String,
                                                  kind As MASDataFormFieldKind) As String
            Return NormalizeText(valueText, String.Empty, MaxValueLength)
        End Function

        Friend Shared Function NormalizeHelperText(helperText As String) As String
            Return NormalizeText(helperText, String.Empty, MaxHelperLength)
        End Function

        Friend Shared Function NormalizeKind(kind As MASDataFormFieldKind) As MASDataFormFieldKind
            If System.Enum.IsDefined(GetType(MASDataFormFieldKind), kind) Then Return kind
            Return MASDataFormFieldKind.Custom
        End Function

        Friend Shared Function NormalizeRequired(required As Boolean,
                                                 state As MASDataFormFieldState) As Boolean
            Return required OrElse state = MASDataFormFieldState.Required
        End Function

        Friend Shared Function NormalizeState(state As MASDataFormFieldState,
                                              required As Boolean) As MASDataFormFieldState
            Dim safeState As MASDataFormFieldState = state
            If Not System.Enum.IsDefined(GetType(MASDataFormFieldState), safeState) Then safeState = MASDataFormFieldState.Normal

            If required AndAlso safeState = MASDataFormFieldState.Normal Then Return MASDataFormFieldState.Required
            Return safeState
        End Function

        Friend Shared Function FormatToggleValue(checked As Boolean) As String
            Return If(checked, "On", "Off")
        End Function

        Friend Shared Function ResolveEmptyValuePlaceholder(kind As MASDataFormFieldKind) As String
            Select Case NormalizeKind(kind)
                Case MASDataFormFieldKind.Toggle
                    Return "Off"
                Case MASDataFormFieldKind.Tags
                    Return "No tags"
                Case MASDataFormFieldKind.Selection
                    Return "Not selected"
                Case Else
                    Return "—"
            End Select
        End Function

        Friend Shared Function IsIssueState(state As MASDataFormFieldState) As Boolean
            Return state = MASDataFormFieldState.Warning OrElse state = MASDataFormFieldState.[Error]
        End Function

        Friend Shared Function HasValidationSignal(state As MASDataFormFieldState,
                                                   required As Boolean,
                                                   helperText As String) As Boolean
            Return NormalizeRequired(required, state) OrElse IsIssueState(state) OrElse NormalizeHelperText(helperText).Length > 0
        End Function

        Friend Shared Function NormalizeText(value As String,
                                             fallback As String,
                                             maxLength As Integer) As String
            Dim text As String = If(value, String.Empty).Trim()
            If text.Length = 0 Then text = If(fallback, String.Empty).Trim()
            If maxLength > 0 AndAlso text.Length > maxLength Then text = text.Substring(0, maxLength)
            Return text
        End Function
    End Class

End Namespace
