Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Components
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Host
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Theming
Imports SkiaSharp

Namespace Nexamas.UI.Application


    Friend NotInheritable Class MASApplicationLayoutControlEntry
        Private ReadOnly _control As MASControlBase
        Private ReadOnly _sizeIntent As MASSize
        Private ReadOnly _alignment As MASLayoutCrossAlignment

        Friend Sub New(control As MASControlBase,
                       sizeIntent As MASSize,
                       alignment As MASLayoutCrossAlignment)
            If control Is Nothing Then Throw New ArgumentNullException(NameOf(control))

            If TypeOf control Is MASMenuBar Then
                ' MASMenuBar is shell-owned application chrome.  Application/page layout
                ' builders are public consumer APIs, so an old page.FullWidth(menuBar) call
                ' must not be able to host or duplicate the MainMenuBar inside content, and
                ' it also must not break retained application-surface resize/apply passes with
                ' a generic LayoutApplicationSurface wrapper exception.  Store a rejected
                ' empty entry: BuildNode/CollectControls become no-ops while lower-level
                ' direct hosting gateways still hard-reject MASMenuBar as content.
                _control = Nothing
                _sizeIntent = Nothing
                _alignment = alignment
                Return
            End If

            _control = control
            ' Keep Nothing when the caller did not provide an explicit entry-level intent.
            ' MASLayoutNode/MASLayoutMeasureEngine then inherit the hosted control's own
            ' MASControlBase.SetSize(...) intent.  Normalizing here would silently force Default
            ' and reintroduce a parallel sizing decision in the Application builder layer.
            _sizeIntent = sizeIntent
            _alignment = alignment
        End Sub

        Friend Function BuildNode() As MASLayoutNode
            If _control Is Nothing Then Return Nothing
            Return MASLayoutBuilder.Item(_control, _sizeIntent, MASLayoutInset.Empty, _alignment)
        End Function

        Friend Sub CollectControls(target As IList(Of MASControlBase))
            If target Is Nothing OrElse _control Is Nothing Then Return
            If Not target.Contains(_control) Then target.Add(_control)
        End Sub
    End Class

End Namespace
