@namespace Lumeo
@implements IDisposable

<div class="@CssClass"
     style="@InlineStyle"
     @attributes="AdditionalAttributes">
    @ChildContent
</div>

@code {
    [CascadingParameter] public Splitter.SplitterContext Context { get; set; } = default!;
    [Parameter] public double Size { get; set; }
    [Parameter] public double MinSize { get; set; } = 10;
    [Parameter] public double MaxSize { get; set; } = 90;
    [Parameter] public bool Collapsible { get; set; }
    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    internal Splitter.SplitterPaneHandle Handle { get; } = new();

    private string CssClass => $"overflow-hidden {Class}".Trim();

    private string InlineStyle
    {
        get
        {
            // Use flex-grow ratios instead of flex-basis% so vertical splitters work without requiring
            // an explicit parent height (% basis resolves to auto when parent height is min-height only).
            var size = Handle.Size.ToString("F4", System.Globalization.CultureInfo.InvariantCulture);
            return $"flex: {size} 1 0; min-width: 0; min-height: 0;";
        }
    }

    protected override void OnInitialized()
    {
        Handle.Size = Size;
        Handle.MinSize = MinSize;
        Handle.MaxSize = MaxSize;
        Context?.RegisterChild(Handle);
    }

    protected override void OnParametersSet()
    {
        // Track min/max updates but don't overwrite runtime-computed size
        Handle.MinSize = MinSize;
        Handle.MaxSize = MaxSize;
    }

    public void Dispose()
    {
        Context?.UnregisterChild(Handle);
    }
}
