@namespace Lumeo

<div role="tooltip" class="@CssClass" @attributes="AdditionalAttributes">
    @ChildContent
    @if (ShowArrow)
    {
        <div class="@ArrowClasses"></div>
    }
</div>

@code {
    [CascadingParameter] public Tooltip.TooltipContext? Context { get; set; }

    [Parameter] public RenderFragment? ChildContent { get; set; }
    [Parameter] public TooltipSide Side { get; set; } = TooltipSide.Top;
    [Parameter] public bool ShowArrow { get; set; } = true;
    [Parameter] public int Offset { get; set; } = 8;
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private bool IsOpen => Context?.IsOpen ?? false;

    private string CssClass => string.Join(" ", new[] { "absolute z-50 overflow-visible rounded-md bg-primary px-3 py-1.5 text-xs text-primary-foreground", AnimationClasses, SideClasses, Class }.Where(s => !string.IsNullOrWhiteSpace(s)));

    private string AnimationClasses => IsOpen
        ? "opacity-100 visible animate-fade-in"
        : "opacity-0 invisible";

    private string SideClasses => Side switch
    {
        TooltipSide.Top => $"bottom-full left-1/2 -translate-x-1/2 mb-{MarginValue}",
        TooltipSide.Bottom => $"top-full left-1/2 -translate-x-1/2 mt-{MarginValue}",
        TooltipSide.Left => $"right-full top-1/2 -translate-y-1/2 mr-{MarginValue}",
        TooltipSide.Right => $"left-full top-1/2 -translate-y-1/2 ml-{MarginValue}",
        _ => $"bottom-full left-1/2 -translate-x-1/2 mb-{MarginValue}"
    };

    private string MarginValue => Offset switch
    {
        <= 4 => "1",
        <= 8 => "2",
        <= 12 => "3",
        _ => "4"
    };

    // Rotated square — same visual as Popover/HoverCard but no border (tooltip is a solid primary surface).
    private string ArrowClasses => Side switch
    {
        TooltipSide.Top => "absolute left-1/2 top-full -translate-x-1/2 -translate-y-1/2 rotate-45 h-2 w-2 bg-primary",
        TooltipSide.Bottom => "absolute left-1/2 bottom-full -translate-x-1/2 translate-y-1/2 rotate-45 h-2 w-2 bg-primary",
        TooltipSide.Left => "absolute top-1/2 left-full -translate-y-1/2 -translate-x-1/2 rotate-45 h-2 w-2 bg-primary",
        TooltipSide.Right => "absolute top-1/2 right-full -translate-y-1/2 translate-x-1/2 rotate-45 h-2 w-2 bg-primary",
        _ => "absolute left-1/2 top-full -translate-x-1/2 -translate-y-1/2 rotate-45 h-2 w-2 bg-primary"
    };

    public enum TooltipSide { Top, Bottom, Left, Right }
}
