Option Strict On
Option Explicit On

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

Namespace Nexamas.UI.Verification

    ''' <summary>
    ''' Deterministic registry that maps Render Verification component keys to owned capture scene builders.
    ''' </summary>
    ''' <remarks>
    ''' The registry is intentionally internal. Gates use it to validate component-key coverage before capture,
    ''' while product capture hosts use it to fail closed when an unregistered component key bypasses the gate.
    ''' </remarks>
    Friend NotInheritable Class MASRenderVerificationCaptureSceneRegistry
        Private ReadOnly _scenes As New Dictionary(Of String, Action(Of MASRenderVerificationCaptureContext, MASRenderVerificationScenario))(StringComparer.OrdinalIgnoreCase)

        Friend Sub Register(componentKey As String,
                            builder As Action(Of MASRenderVerificationCaptureContext, MASRenderVerificationScenario))
            Dim normalized As String = If(componentKey, String.Empty).Trim()
            If String.IsNullOrWhiteSpace(normalized) Then Throw New ArgumentException("A render-verification component key is required.", NameOf(componentKey))
            If builder Is Nothing Then Throw New ArgumentNullException(NameOf(builder))
            If _scenes.ContainsKey(normalized) Then Throw New InvalidOperationException("Render-verification capture scene is already registered for component key '" & normalized & "'.")
            _scenes.Add(normalized, builder)
        End Sub

        Friend ReadOnly Property Count As Integer
            Get
                Return _scenes.Count
            End Get
        End Property

        Friend ReadOnly Property Keys As IReadOnlyList(Of String)
            Get
                Return New ReadOnlyCollection(Of String)(New List(Of String)(_scenes.Keys))
            End Get
        End Property

        Friend Function Contains(componentKey As String) As Boolean
            Dim normalized As String = If(componentKey, String.Empty).Trim()
            Return normalized.Length > 0 AndAlso _scenes.ContainsKey(normalized)
        End Function

        Friend Sub Build(componentKey As String,
                         context As MASRenderVerificationCaptureContext,
                         scenario As MASRenderVerificationScenario)
            If context Is Nothing Then Throw New ArgumentNullException(NameOf(context))
            If scenario Is Nothing Then Throw New ArgumentNullException(NameOf(scenario))

            Dim normalized As String = If(componentKey, String.Empty).Trim()
            Dim builder As Action(Of MASRenderVerificationCaptureContext, MASRenderVerificationScenario) = Nothing
            If Not _scenes.TryGetValue(normalized, builder) Then
                Throw New InvalidOperationException("No render-verification capture scene is registered for component key '" & normalized & "'.")
            End If

            builder(context, scenario)
        End Sub
    End Class

End Namespace
