@namespace Oqtane.UI

<CascadingValue Value="@ModuleState" IsFixed="true">
    @if (_useadminborder)
    {
        <div class="app-pane-admin-border">
            @DynamicComponent
        </div>
    }
    else
    {
        @DynamicComponent
    }
</CascadingValue>

@code {
    private bool _useadminborder = false;

    [CascadingParameter]
    protected PageState PageState { get; set; }

    [Parameter]
    public Module ModuleState { get; set; }

    RenderFragment DynamicComponent { get; set; }

    protected override void OnParametersSet()
    {
        string container = ModuleState.ContainerType;
        if (PageState.ModuleId != -1 && ModuleState.UseAdminContainer)
        {
            container = (!string.IsNullOrEmpty(PageState.Site.AdminContainerType)) ? PageState.Site.AdminContainerType : Constants.DefaultAdminContainer;
        }

        if (PageState.EditMode && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.Permissions) && PageState.Action == Constants.DefaultAction)
        {
            _useadminborder = true;
        }
        else
        {
            _useadminborder = false;
        }

        DynamicComponent = builder =>
        {
            Type containerType = Type.GetType(container);
            if (containerType == null)
            {
                // fallback
                containerType = Type.GetType(Constants.DefaultContainer);
            }
            builder.OpenComponent(0, containerType);
            builder.CloseComponent();
        };
    }
}
