@using Wave.Utilities
@implements IDisposable
@inject IMessageDisplay Messages
@rendermode InteractiveServer

<div class="toast toast-start z-10 whitespace-normal" role="alert">
	@foreach (var message in MessagesDisplayed) {
		<div class="alert @message.Type max-w-sm" @onclick="() => MessagesDisplayed.Remove(message)">
			@if (message.Title is null) {
				@message.Body
			} else {
				<span class="font-bold">@message.Title</span>
				<span><small>@message.Body</small></span>
			}
		</div>
	}
</div>

@code {
	private List<IMessageDisplay.Message> MessagesDisplayed { get; } = [];

	protected override void OnInitialized() {
		Messages.OnMessage += OnMessage;
	}

	private bool OnMessage(IMessageDisplay.Message message) {
		MessagesDisplayed.Add(message);
		InvokeAsync(StateHasChanged);
		return true;
	}

	public void Dispose() {
		Messages.OnMessage -= OnMessage;
	}
}
