"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.findLocatorWithPagination = void 0;
async function findLocatorWithPagination(page, locator, options) {
    const { timeout = 30000 } = options || {};
    const maxPages = 10;
    const paginationButton = page.getByRole('button', { name: 'Next page' });
    const backupPaginationButton = page.getByRole('button', { name: 'Load more' });
    const startTime = Date.now();
    for (let pageNum = 0; pageNum < maxPages; pageNum++) {
        if (Date.now() - startTime > timeout) {
            console.log(`Overall timeout of ${timeout}ms exceeded`);
            break;
        }
        const textExists = await locator
            .waitFor({ state: 'visible', timeout: 5000 })
            .then(() => true)
            .catch(() => false);
        if (textExists) {
            console.log(`Found locator "${locator}" on page ${pageNum + 1}`);
            return true;
        }
        const nextButton = paginationButton.or(backupPaginationButton).last();
        const isVisible = await nextButton
            .isVisible({ timeout: 1000 })
            .catch(() => false);
        const isEnabled = isVisible
            ? await nextButton.isEnabled().catch(() => false)
            : false;
        if (!isVisible || !isEnabled) {
            console.log(`No more pages available after page ${pageNum + 1} (visible: ${isVisible}, enabled: ${isEnabled})`);
            break;
        }
        console.log(`Locator "${locator}" not found on page ${pageNum + 1}, going to next page...`);
        await nextButton.click();
        await page.waitForTimeout(1000);
    }
    return false;
}
exports.findLocatorWithPagination = findLocatorWithPagination;
