@namespace Lumeo

<button type="button" class="@CssClass" aria-expanded="@IsOpen" disabled="@IsDisabled" @onclick="Toggle" @attributes="AdditionalAttributes">
    @ChildContent
    <Blazicon Svg="Lucide.ChevronDown" class="@IconClasses" />
</button>

@code {
    [CascadingParameter] public Accordion.AccordionContext Context { get; set; } = default!;
    [CascadingParameter] public AccordionItem.AccordionItemContext ItemContext { get; set; } = default!;
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private bool IsOpen => Context.IsOpen(ItemContext.Value);
    private bool IsDisabled => ItemContext.Disabled;

    private string CssClass => $"flex flex-1 w-full items-center justify-between py-4 text-sm font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 rounded-sm {(IsDisabled ? "cursor-not-allowed" : "cursor-pointer hover:underline")} {Class}".Trim();

    private string IconClasses => string.Join(" ", new[] { "h-4 w-4 shrink-0 text-muted-foreground transition-transform duration-200", IsOpen ? "rotate-180" : null }.Where(s => !string.IsNullOrWhiteSpace(s)));

    private async Task Toggle()
    {
        if (IsDisabled) return;
        await Context.Toggle.InvokeAsync(ItemContext.Value);
    }
}
