@inherits LayoutComponentBase
@implements IDisposable
@inject RolemasterDb.App.Frontend.AppState.ThemeState ThemeState

<AppShell>
    @Body
</AppShell>

<div id="blazor-error-ui" data-nosnippet>
    An unhandled error has occurred.
    <a href="." class="reload">Reload</a>
    <span class="dismiss">🗙</span>
</div>

@code {
    protected override void OnInitialized()
    {
        ThemeState.Changed += HandleThemeChanged;
    }

    protected override Task OnAfterRenderAsync(bool firstRender) =>
        firstRender
            ? ThemeState.InitializeAsync()
            : Task.CompletedTask;

    private void HandleThemeChanged()
    {
        _ = InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        ThemeState.Changed -= HandleThemeChanged;
    }
}
