namespace Tests.Shared;

public class ToastComponent
{
    private readonly Website _website;

    public ToastComponent(Website website)
    {
        _website = website;
    }

    public ILocator Container => _website.Locator(".toastsContainer");
    public ILocator Toasts => _website.Locator(".toastsContainer .toastContainer");

    public async Task<IReadOnlyList<string>> GetToastTitlesAsync()
    {
        var titles = await _website.Locator(".toastsContainer .toastTitle").AllTextContentsAsync();
        return titles.Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)).ToList();
    }

    public async Task<bool> HasToastContainingAsync(string text)
    {
        try
        {
            await _website.Page.WaitForFunctionAsync(
                @"(expected) => {
                    const titles = document.querySelectorAll('.toastsContainer .toastTitle');
                    return Array.from(titles).some(t => t.textContent.trim().includes(expected));
                }",
                text,
                new PageWaitForFunctionOptions { Timeout = 3000 }
            );
            return true;
        }
        catch
        {
            return false;
        }
    }
}