"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertReportWithRefreshes = exports.createReportForProcess = void 0;
const UtilitiesPage_1 = require("../SM-8.10/UtilitiesPage");
const UtilitiesPage_2 = require("../SM-8.10/UtilitiesPage");
const createReportForProcess = async (optimizeCollectionsPage, optimizeReportPage, processName) => {
    const maxRetries = 30;
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            await optimizeCollectionsPage.clickCreateNewButton();
            await optimizeCollectionsPage.clickReportOption();
            await optimizeReportPage.clickProcessSelectionButton();
            await optimizeReportPage.waitUntilUserTaskProcessIsVisible(processName);
            return;
        }
        catch (error) {
            try {
                await optimizeReportPage.clickCancelButton();
            }
            catch (cancelError) {
                // Page may have been closed due to test timeout, ignore
            }
            await new Promise((resolve) => setTimeout(resolve, 2000));
        }
    }
    throw new Error(`Failed to create report for process ${processName} after ${maxRetries} attempts`);
};
exports.createReportForProcess = createReportForProcess;
const assertReportWithRefreshes = async (page, optimizeHomePage, optimizeCollectionsPage, optimizeReportPage, reportName, locator, text) => {
    // Was 30. Each attempt does 4 nested retries (each ~30s), so 30
    // worst-case = 15 min on the longest-pole shard. Test that's
    // genuinely failing at 30 attempts will also fail at 10 — same
    // outcome but 3× faster fail. Test that needs more than 10
    // refresh cycles for Optimize to catch up has a bigger underlying
    // problem worth surfacing.
    const maxRetries = 10;
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        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);
            return;
        }
        catch (error) {
            // nothing to do. Just loop back to the collections
        }
    }
    throw new Error(`Failed to assert report ${reportName} after ${maxRetries} attempts`);
};
exports.assertReportWithRefreshes = assertReportWithRefreshes;
