using Microsoft.AspNetCore.Razor.TagHelpers;
using PlanTempus.Application.Features.Localization.Services;

namespace PlanTempus.Application.Features._Shared.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");
    }
}
