@using System.Web
@if (!string.IsNullOrWhiteSpace(viewerUrl))
{
    <a href="@viewerUrl" target="_blank" class="citation">
        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
        </svg>
        <div class="citation-content">
            <div class="citation-file">@File</div>
            <div>@Quote</div>
        </div>
    </a>
}

@code {
    [Parameter]
    public required string File { get; set; }

    [Parameter]
    public int? PageNumber { get; set; }

    [Parameter]
    public required string Quote { get; set; }

    private string? viewerUrl;

    protected override void OnParametersSet()
    {
        viewerUrl = null;

        // If you ingest other types of content besides PDF files, construct a URL to an appropriate viewer here
        if (File.EndsWith(".pdf"))
        {
            var search = Quote?.Trim('.', ',', ' ', '\n', '\r', '\t', '"', '\'');
            viewerUrl = $"lib/pdf_viewer/viewer.html?file=/Data/{HttpUtility.UrlEncode(File)}#page={PageNumber}&search={HttpUtility.UrlEncode(search)}&phrase=true";
        }
    }
}
