Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Components

    ''' <summary>
    ''' Friend-only policy that builds host-adoption proofs from existing boundary,
    ''' hosted-content, layout, and interaction snapshots. It is not a Router, not a
    ''' PageHost, not a native shell adapter, and not a public API surface.
    ''' </summary>
    Friend NotInheritable Class MASRouteShellHostAdoptionPolicy
        Private Sub New()
        End Sub

        Friend Shared Function CreateAppLayoutAdoption(boundary As MASRouteShellBoundarySnapshot,
                                                       layoutSnapshot As MASAppLayoutSnapshot,
                                                       hostedSnapshot As MASHostedContentContractSnapshot) As MASRouteShellHostAdoptionSnapshot
            Dim safeBoundary As Boolean = MASRouteShellBoundaryPolicy.IsSafeClosedBoundary(boundary,
                                                                                           "applayout",
                                                                                           "AppLayout.RoutingOwnership",
                                                                                           MASRouteShellBoundaryKind.AppLayoutRouteOwnership)
            Return New MASRouteShellHostAdoptionSnapshot(
                1,
                "applayout",
                "AppLayout.ResponsiveShellAdoption",
                MASRouteShellBoundaryKind.AppLayoutRouteOwnership,
                hostConsumesBoundarySnapshot:=safeBoundary,
                hasLayoutSnapshot:=layoutSnapshot IsNot Nothing AndAlso layoutSnapshot.Version > 0,
                hasHostedContentSnapshot:=hostedSnapshot IsNot Nothing AndAlso hostedSnapshot.IsStructurallyValid AndAlso hostedSnapshot.HostedSlotCount > 0,
                hasInteractionSnapshot:=False,
                keepsRouterExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsRouter,
                keepsPageHostExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsPageHost,
                keepsNativeShellExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsNativeShell,
                keepsPublicAdapterExternal:=boundary IsNot Nothing AndAlso Not boundary.ExposesPublicAdapter,
                capturesPersistenceIntent:=True,
                capturesFocusIntent:=False,
                capturesAnnouncementIntent:=False,
                capturesResponsiveIntent:=True,
                note:="Application host can adopt AppLayout slot/layout snapshots and responsive breakpoint intent while keeping route, PageHost, native shell, persistence, and public adapter ownership outside AppLayout.")
        End Function

        Friend Shared Function CreateSplitViewAdoption(boundary As MASRouteShellBoundarySnapshot,
                                                       layoutSnapshot As MASSplitViewLayoutSnapshot,
                                                       hostedSnapshot As MASHostedContentContractSnapshot,
                                                       splitterSnapshot As MASSplitViewKeyboardSplitterSnapshot) As MASRouteShellHostAdoptionSnapshot
            Dim safeBoundary As Boolean = MASRouteShellBoundaryPolicy.IsSafeClosedBoundary(boundary,
                                                                                           "splitview",
                                                                                           "SplitView.ShellIntegration",
                                                                                           MASRouteShellBoundaryKind.SplitViewShellIntegrationBoundary)
            Return New MASRouteShellHostAdoptionSnapshot(
                1,
                "splitview",
                "SplitView.HostAdoptionProof",
                MASRouteShellBoundaryKind.SplitViewShellIntegrationBoundary,
                hostConsumesBoundarySnapshot:=safeBoundary,
                hasLayoutSnapshot:=layoutSnapshot IsNot Nothing AndAlso layoutSnapshot.Version > 0,
                hasHostedContentSnapshot:=hostedSnapshot IsNot Nothing AndAlso hostedSnapshot.IsStructurallyValid AndAlso hostedSnapshot.HostedSlotCount > 0,
                hasInteractionSnapshot:=splitterSnapshot IsNot Nothing AndAlso splitterSnapshot.HasKeyboardSplitterContract,
                keepsRouterExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsRouter,
                keepsPageHostExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsPageHost,
                keepsNativeShellExternal:=boundary IsNot Nothing AndAlso Not boundary.OwnsNativeShell,
                keepsPublicAdapterExternal:=boundary IsNot Nothing AndAlso Not boundary.ExposesPublicAdapter,
                capturesPersistenceIntent:=True,
                capturesFocusIntent:=True,
                capturesAnnouncementIntent:=True,
                capturesResponsiveIntent:=False,
                note:="Application host can adopt SplitView pane/splitter snapshots for persistence intent, focus coordination, and announcements while keeping shell ownership outside SplitView.")
        End Function

        Friend Shared Function IsSafeHostAdoption(snapshot As MASRouteShellHostAdoptionSnapshot,
                                                 expectedOwner As String,
                                                 expectedEvidence As String,
                                                 expectedKind As MASRouteShellBoundaryKind) As Boolean
            If snapshot Is Nothing Then Return False
            Return snapshot.IsSafeHostAdoptionProof AndAlso
                   String.Equals(snapshot.OwnerKey, expectedOwner, System.StringComparison.OrdinalIgnoreCase) AndAlso
                   String.Equals(snapshot.EvidenceKey, expectedEvidence, System.StringComparison.OrdinalIgnoreCase) AndAlso
                   snapshot.BoundaryKind = expectedKind
        End Function
    End Class

End Namespace
