@namespace Lumeo

@{
    var context = new MenubarContext(_openMenuId, EventCallback.Factory.Create<string?>(this, SetOpenMenuId), _openMenuId is not null, _menuIds, RegisterMenuId, NavigateMenus);
}

<CascadingValue Value="context" IsFixed="false">
    <div role="menubar" class="@CssClass" @attributes="AdditionalAttributes">
        @ChildContent
    </div>
</CascadingValue>

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private string? _openMenuId;
    private readonly List<string> _menuIds = [];

    private string CssClass => $"flex h-9 items-center space-x-1 rounded-md border border-border bg-background p-1 shadow-sm {Class}".Trim();

    private void SetOpenMenuId(string? menuId)
    {
        _openMenuId = menuId;
        StateHasChanged();
    }

    private void RegisterMenuId(string menuId)
    {
        if (!_menuIds.Contains(menuId))
        {
            _menuIds.Add(menuId);
            StateHasChanged();
        }
    }

    private void NavigateMenus(int direction)
    {
        if (_menuIds.Count == 0) return;
        if (_openMenuId is null)
        {
            _openMenuId = direction > 0 ? _menuIds[0] : _menuIds[^1];
        }
        else
        {
            var currentIndex = _menuIds.IndexOf(_openMenuId);
            if (currentIndex < 0) return;
            var nextIndex = (currentIndex + direction + _menuIds.Count) % _menuIds.Count;
            _openMenuId = _menuIds[nextIndex];
        }
        StateHasChanged();
    }

    public record MenubarContext(
        string? OpenMenuId,
        EventCallback<string?> SetOpenMenuId,
        bool AnyOpen,
        List<string> MenuIds,
        Action<string> RegisterMenuId,
        Action<int> NavigateMenus);
}
