"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertReportWithRefreshes = exports.createReportForProcess = void 0;
const UtilitiesPage_1 = require("../SM-8.8/UtilitiesPage");
const createReportForProcess = async (page, optimizeCollectionsPage, optimizeReportPage, processName) => {
    const TOTAL_TIMEOUT_MS = process.env.IS_MT === 'true' ? 10 * 60 * 1000 : 5 * 60 * 1000; // 10 min for MT (slower ingestion), 5 min otherwise
    const startTime = Date.now();
    let attempt = 0;
    let found = false;
    while (Date.now() - startTime < TOTAL_TIMEOUT_MS) {
        attempt++;
        try {
            await optimizeCollectionsPage.clickCreateNewButton();
            await optimizeCollectionsPage.clickReportOption();
            await optimizeReportPage.clickProcessSelectionButton();
            await optimizeReportPage.waitUntilUserTaskProcessIsVisible(processName);
            found = true;
            break;
        }
        catch (error) {
            try {
                await optimizeReportPage.clickCancelButton();
            }
            catch (cancelError) {
                // Page may have been closed due to test timeout, ignore
            }
            console.log(`createReportForProcess attempt ${attempt} failed for "${processName}". Reloading page...`);
            await page.reload();
            await page.waitForLoadState('load', { timeout: 10000 }).catch(() => {
                // Safe to ignore: reload may trigger slow load, we'll retry the whole flow
            });
            await new Promise((resolve) => setTimeout(resolve, 3000));
        }
    }
    if (!found) {
        throw new Error(`createReportForProcess: Process "${processName}" not found after ${attempt} attempts (${TOTAL_TIMEOUT_MS / 1000}s timeout)`);
    }
};
exports.createReportForProcess = createReportForProcess;
const assertReportWithRefreshes = async (page, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, reportName, locator, text) => {
    const TOTAL_TIMEOUT_MS = process.env.IS_MT === 'true' ? 10 * 60 * 1000 : 5 * 60 * 1000; // 10 min for MT (slower ingestion), 5 min otherwise
    const startTime = Date.now();
    let attempt = 0;
    let found = false;
    while (Date.now() - startTime < TOTAL_TIMEOUT_MS) {
        attempt++;
        try {
            await optimizeHomePage.clickCollectionsLink();
            await (0, UtilitiesPage_1.assertPageTextWithRetry)(page, reportName, false);
            await optimizeCollectionsPage.clickMostRecentProcessReport(reportName);
            await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(page, locator, text, 0);
            found = true;
            break;
        }
        catch (error) {
            console.log(`assertReportWithRefreshes attempt ${attempt} failed for "${reportName}". Reloading page...`);
            await page.reload();
            await page.waitForLoadState('load', { timeout: 10000 }).catch(() => {
                // Safe to ignore: reload may trigger slow load, we'll retry the whole flow
            });
            await new Promise((resolve) => setTimeout(resolve, 3000));
        }
    }
    if (!found) {
        throw new Error(`assertReportWithRefreshes: Report "${reportName}" assertion failed after ${attempt} attempts (${TOTAL_TIMEOUT_MS / 1000}s timeout)`);
    }
};
exports.assertReportWithRefreshes = assertReportWithRefreshes;
