using HtmlAgilityPack;

namespace JSMR.Infrastructure.Http;

public class HtmlLoader(IHttpService httpService) : IHtmlLoader
{
    public async Task<HtmlLoadResult> GetHtmlDocumentAsync(string url, CancellationToken cancellationToken)
    {
        HttpStringResponse response = await httpService.GetAsync(url, cancellationToken);

        if (!response.IsSuccessStatusCode)
        {
            return new HtmlLoadResult
            {
                StatusCode = response.StatusCode,
                Document = null
            };
        }

        HtmlDocument document = new();
        document.LoadHtml(response.Content ?? string.Empty);

        return new HtmlLoadResult
        {
            StatusCode = response.StatusCode,
            Document = document
        };
    }
}