using Microsoft.AspNetCore.Razor.TagHelpers;
using CalendarServer.Features.Language.Services;

namespace CalendarServer.Features.Language.TagHelpers;

[HtmlTargetElement(Attributes = "localize")]
public class LocalizeTagHelper : TagHelper
{
    private readonly ILocalizationService _localize;

    public LocalizeTagHelper(ILocalizationService localize)
    {
        _localize = localize;
    }

    [HtmlAttributeName("localize")]
    public string Key { get; set; } = string.Empty;

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        var translated = _localize.Get(Key);

        if (!string.IsNullOrEmpty(translated) && translated != Key)
        {
            output.Content.SetContent(translated);
        }

        output.Attributes.RemoveAll("localize");
    }
}
