"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptimizeCollectionsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OptimizeCollectionsPage {
    page;
    collectionsPageHeader;
    createNewButton;
    reportOption;
    modalCloseButton;
    processReport;
    collectionsLink;
    constructor(page) {
        this.page = page;
        this.collectionsPageHeader = page.getByRole('heading', {
            name: 'Collections, Dashboards, and Reports',
        });
        this.createNewButton = page.getByRole('button', {
            name: 'Create new',
            exact: true,
        });
        this.reportOption = page.getByText('Report', { exact: true });
        this.modalCloseButton = page.locator('button').filter({ hasText: /^Close$/ });
        this.processReport = page.getByText('Process Report').first();
        this.collectionsLink = page
            .getByRole('banner')
            .getByRole('link', { name: 'Collections' });
    }
    async clickCollectionsLink() {
        await (0, test_1.expect)(this.collectionsLink).toBeVisible({ timeout: 60000 });
        await this.collectionsLink.click({ timeout: 60000 });
    }
    async clickCreateNewButton() {
        await (0, test_1.expect)(this.createNewButton).toBeVisible({ timeout: 60000 });
        await this.createNewButton.click({ timeout: 60000 });
    }
    async clickCreateNewButtonWithRetry() {
        const maxRetries = 5;
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            try {
                if (await this.modalCloseButton.isVisible({ timeout: 15000 })) {
                    await this.modalCloseButton.click({ timeout: 60000 });
                }
                await this.clickCollectionsLink();
                await this.clickCreateNewButton();
                return;
            }
            catch (error) {
                if (attempt < maxRetries - 1) {
                    console.warn(`Attempt ${attempt + 1} failed. Retrying...`);
                    await this.page.reload();
                    await (0, sleep_1.sleep)(60000);
                }
                else {
                    throw new Error(`Assertion failed after ${maxRetries} attempts: ` + error);
                }
            }
        }
    }
    async clickReportOption() {
        await this.reportOption.click();
    }
    async clickMostRecentProcessReport(reportName) {
        await this.page.getByText(reportName).click({ timeout: 60000 });
    }
}
exports.OptimizeCollectionsPage = OptimizeCollectionsPage;
