using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace SelectPdf.Samples
{
    // Base for the convert pages. Drives the "Working …" button state while a
    // conversion runs.
    //
    // Unlike the Razor Pages sample (classic form POST → file download, handled
    // with JS + a download-token cookie), a Blazor InteractiveServer submit is
    // run as async C# over the circuit — and the SelectPdf conversion is
    // synchronous and BLOCKS the circuit. So the busy state must be driven in
    // C#: flip Busy, force a render + yield so the button repaints as
    // "Working …" BEFORE the blocking work starts, run the work, then clear
    // Busy in a finally (the trailing re-render restores the original label).
    // The guard also prevents double-submit while a conversion is in flight.
    public abstract class SampleFormBase : ComponentBase
    {
        protected bool Busy { get; private set; }

        protected async Task SubmitAsync(Func<Task> work)
        {
            if (Busy) return;
            Busy = true;
            StateHasChanged();
            await Task.Delay(1);   // let "Working …" paint before the blocking convert
            try
            {
                await work();
            }
            finally
            {
                Busy = false;
            }
        }
    }
}
