"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertReportWithRefreshes = exports.createReportForProcess = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../SM-8.9/UtilitiesPage");
const UtilitiesPage_2 = require("../SM-8.9/UtilitiesPage");
const createReportForProcess = async (page, optimizeCollectionsPage, optimizeReportPage, processName) => {
    let retries = 1000; // big number as this can struggle when many processes are running at once
    while (retries > 0) {
        try {
            await optimizeCollectionsPage.clickCreateNewButton();
            await optimizeCollectionsPage.clickReportOption();
            await optimizeReportPage.clickProcessSelectionButton();
            await (0, test_1.expect)(page.getByText(processName, { exact: true })).toBeVisible({
                timeout: 30000,
            });
            break;
        }
        catch (error) {
            try {
                await optimizeReportPage.clickCancelButton();
            }
            catch (cancelError) {
                // Page may have been closed due to test timeout, ignore
            }
            await page.reload();
            await page.waitForLoadState('load', { timeout: 10000 }).catch(() => { });
            await new Promise((resolve) => setTimeout(resolve, 3000));
        }
        retries--;
        (0, test_1.expect)(retries).toBeGreaterThan(0);
    }
};
exports.createReportForProcess = createReportForProcess;
const assertReportWithRefreshes = async (page, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, reportName, locator, text) => {
    let retries = 1000; // big number as this can struggle when many processes are running at once
    while (retries > 0) {
        try {
            await optimizeHomePage.clickCollectionsLink();
            await (0, UtilitiesPage_1.assertPageTextWithRetry)(page, reportName, false);
            await optimizeCollectionsPage.clickMostRecentProcessReport(reportName);
            await (0, UtilitiesPage_2.assertLocatorVisibleWithRetry)(page, locator, text, 0);
            break;
        }
        catch (error) {
            try {
                await page.reload();
                await page.waitForLoadState('load', { timeout: 10000 }).catch(() => { });
            }
            catch (reloadError) {
                // Page may have been detached, ignore and retry
            }
            await new Promise((resolve) => setTimeout(resolve, 3000));
        }
        retries--;
        (0, test_1.expect)(retries).toBeGreaterThan(0);
    }
};
exports.assertReportWithRefreshes = assertReportWithRefreshes;
