"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectLocatorWithPagination = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../sleep");
const findLocatorWithPagination_1 = require("./findLocatorWithPagination");
async function expectLocatorWithPagination(page, locator, options) {
    const { shouldBeVisible = true, totalTimeout = 30000, maxRetries = 5, } = options || {};
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            if (options?.clickLocator) {
                await (0, test_1.expect)(options?.clickLocator).toBeVisible({
                    timeout: 30000,
                });
                await options?.clickLocator.click({
                    timeout: 30000,
                });
                await (0, sleep_1.sleep)(1000);
            }
            const found = await (0, findLocatorWithPagination_1.findLocatorWithPagination)(page, locator);
            if (shouldBeVisible && !found) {
                throw new Error(`Locator "${locator}" not found on any page within ${totalTimeout}ms`);
            }
            if (!shouldBeVisible && found) {
                throw new Error(`Locator "${locator}" should not exist but was found`);
            }
            if (shouldBeVisible && found) {
                await (0, test_1.expect)(locator).toBeVisible({ timeout: 5000 });
            }
            return;
        }
        catch (error) {
            if (attempt < maxRetries - 1) {
                console.warn(`Attempt ${attempt + 1} failed for asserting ${locator}. Retrying...`);
                if (!page.isClosed()) {
                    await page.waitForLoadState('domcontentloaded');
                    await page.reload();
                    await (0, sleep_1.sleep)(1000);
                }
            }
            else {
                throw new Error(`Assertion ${locator} failed after ${maxRetries} attempts` + error);
            }
        }
    }
}
exports.expectLocatorWithPagination = expectLocatorWithPagination;
