"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptimizeCollectionsPage = void 0;
class OptimizeCollectionsPage {
    page;
    collectionsPageHeader;
    createNewButton;
    reportOption;
    modalCloseButton;
    processReport;
    collectionsLink;
    reportAuthor;
    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.getByText('Close', { exact: true });
        this.processReport = page.getByText('Process Report').first();
        this.collectionsLink = page.getByRole('link', { name: 'Collections' });
        this.reportAuthor = page.getByRole('cell', { name: 'Demo User' }).first();
    }
    async clickCreateNewButton() {
        try {
            await this.modalCloseButton.click({ timeout: 60000 });
            await this.collectionsLink.click({ timeout: 60000 });
            await this.createNewButton.click({ timeout: 90000 });
        }
        catch (error) {
            await this.collectionsLink.click({ timeout: 60000 });
            await this.createNewButton.click({ timeout: 90000 });
        }
    }
    async clickReportOption() {
        await this.reportOption.click();
    }
    async clickMostRecentProcessReport(reportName) {
        await this.page.getByText(reportName).click({ timeout: 60000 });
    }
    async clickFirstProcessReport() {
        const cells = await this.page
            .getByRole('cell', { name: /Process report/ })
            .all();
        if (cells.length === 0) {
            throw new Error('No process report cells found');
        }
        const cell = cells[0];
        const text = (await cell.textContent())?.trim();
        if (!text) {
            throw new Error('Could not read cell text');
        }
        const match = text.match(/^(\S+)\s*Process report$/i);
        if (!match) {
            throw new Error(`Unexpected text format: ${text}`);
        }
        const dynamicValue = match[1];
        await cell.click();
        await this.page.getByRole('link', { name: dynamicValue }).click();
    }
}
exports.OptimizeCollectionsPage = OptimizeCollectionsPage;
