@namespace Lumeo

@* The `group` class + `data-state` attribute let consumers rotate a chevron on open with
   `group-data-[state=open]:rotate-180` on any descendant icon — no EventCallbacks or bindings needed. *@
<div id="@_triggerId"
     class="@CssClass"
     role="button"
     data-state="@(Context.IsOpen ? "open" : "closed")"
     aria-haspopup="menu"
     aria-expanded="@Context.IsOpen.ToString().ToLower()"
     aria-controls="@Context.ContentId"
     @onclick="Toggle"
     @attributes="AdditionalAttributes">
    @ChildContent
</div>

@code {
    [CascadingParameter] public DropdownMenu.DropdownMenuContext Context { 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; }

    internal string TriggerId => _triggerId;
    private readonly string _triggerId = $"dropdown-trigger-{Guid.NewGuid():N}";

    private string CssClass => $"group inline-flex {Class}".Trim();

    private async Task Toggle() => await Context.SetOpen.InvokeAsync(!Context.IsOpen);
}
