@namespace SufiChain.SufiBlazor.Components.Common
@using Markdig

<div class="@CssClass" style="@Style" @attributes="AdditionalAttributes">
    @((MarkupString)_renderedHtml)
</div>

@code {
    private string _renderedHtml = string.Empty;
    private static readonly MarkdownPipeline _pipeline = new MarkdownPipelineBuilder()
        .UseAdvancedExtensions()
        .Build();
    
    /// <summary>
    /// The markdown content to render.
    /// </summary>
    [Parameter] public string? Content { get; set; }
    
    /// <summary>
    /// Additional CSS classes to apply.
    /// </summary>
    [Parameter] public string? Class { get; set; }
    
    /// <summary>
    /// Inline styles to apply.
    /// </summary>
    [Parameter] public string? Style { get; set; }
    
    /// <summary>
    /// Additional attributes to apply to the root element.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object>? AdditionalAttributes { get; set; }
    
    private string CssClass => string.IsNullOrWhiteSpace(Class) 
        ? "sb-markdown-viewer" 
        : $"sb-markdown-viewer {Class}";
    
    protected override void OnParametersSet()
    {
        _renderedHtml = string.IsNullOrEmpty(Content) 
            ? string.Empty 
            : Markdown.ToHtml(Content, _pipeline);
    }
}
