"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertReportWithRefreshes = exports.createReportForProcess = void 0;
const UtilitiesPage_1 = require("../SM-8.9/UtilitiesPage");
const UtilitiesPage_2 = require("../SM-8.9/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) => {
    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;
