@using Moonlight.App.Services
@using Moonlight.App.Services.Interop

@inject SmartTranslateService SmartTranslateService
@inject AlertService AlertService

@if (!Working)
{
    <button class="btn btn-danger" @onclick="Do">
        <i class="bx bx-trash"></i>
    </button>
}
else
{
    <button class="btn btn-danger disabled" disabled="">
        <span class="spinner-border spinner-border-sm align-middle me-2"></span>
    </button>
}

@code
{
    private bool Working { get; set; } = false;

    [Parameter]
    public Func<Task>? OnClick { get; set; }

    [Parameter]
    public bool Confirm { get; set; } = false;

    private async Task Do()
    {
        Working = true;
        StateHasChanged();
        await Task.Run(async () =>
        {
            if (Confirm)
            {
                var b = await AlertService.ConfirmMath();

                if (b)
                {
                    if(OnClick != null)
                        await OnClick.Invoke();
                }
                    
            }

            Working = false;
            await InvokeAsync(StateHasChanged);
        });
    }
}