@namespace Lumeo

@{
    var context = new ContextMenuContext(Open, _x, _y, EventCallback.Factory.Create<bool>(this, SetOpen), EventCallback.Factory.Create<(double, double)>(this, SetPosition));
}

<CascadingValue Value="context" IsFixed="false">
    <div class="relative inline-block" @attributes="AdditionalAttributes">
        @ChildContent
    </div>
</CascadingValue>

@code {
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public bool Open { get; set; }
    [Parameter] public EventCallback<bool> OpenChanged { get; set; }

    [Obsolete("Use Open instead. IsOpen will be removed in a future release.")]
    [Parameter] public bool IsOpen { get => Open; set => Open = value; }

    [Obsolete("Use OpenChanged instead. IsOpenChanged will be removed in a future release.")]
    [Parameter] public EventCallback<bool> IsOpenChanged { get => OpenChanged; set => OpenChanged = value; }

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

    private double _x;
    private double _y;

    private async Task SetOpen(bool value)
    {
        Open = value;
        await OpenChanged.InvokeAsync(value);
    }

    private Task SetPosition((double X, double Y) pos)
    {
        _x = pos.X;
        _y = pos.Y;
        return Task.CompletedTask;
    }

    public record ContextMenuContext(bool IsOpen, double X, double Y, EventCallback<bool> SetOpen, EventCallback<(double, double)> SetPosition);
}
