@using Iceshrimp.Frontend.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Localization> Loc;

<span title="@Date.ToLocalTime().ToString("G")">@RenderTimestamp()</span>

@code {
    [Parameter, EditorRequired] public required DateTime Date             { get; set; }

    private string RenderTimestamp()
    {
        var diff = DateTime.UtcNow - Date.ToUniversalTime();

        return diff switch
        {
            { TotalDays: >= 365 }  => Loc["{0}y", Math.Floor(diff.TotalDays / 365)],
            { TotalDays: >= 30 }   => Loc["{0}mo", Math.Floor(diff.TotalDays / 30)],
            { TotalDays: >= 7 }    => Loc["{0}w", Math.Floor(diff.TotalDays / 7)],
            { TotalDays: >= 1 }    => Loc["{0}d", Math.Floor(diff.TotalDays)],
            { TotalHours: >= 1 }   => Loc["{0}h", Math.Floor(diff.TotalHours)],
            { TotalMinutes: >= 1 } => Loc["{0}m", Math.Floor(diff.TotalMinutes)],
            { TotalSeconds: >= 5 } => Loc["{0}s", Math.Floor(diff.TotalSeconds)],
            _                      => Loc["now"]
        };
    }
}