"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;
    collectionOption;
    modalCloseButton;
    processReport;
    collectionsLink;
    collectionNameInput;
    createCollectionConfirmButton;
    collectionContextMenuButton;
    editContextMenuOption;
    deleteContextMenuOption;
    confirmDeleteButton;
    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.collectionOption = page
            .getByRole('menuitem', { name: 'Collection', exact: true })
            .or(page.getByRole('button', { name: 'Collection', 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' });
        this.collectionNameInput = page
            .getByRole('dialog')
            .getByRole('textbox')
            .first();
        this.createCollectionConfirmButton = page
            .getByRole('dialog')
            .getByRole('button', { name: /add data sources|create collection|save/i })
            .or(page.locator('.cds--modal button.confirm.cds--btn--primary'))
            .first();
        this.collectionContextMenuButton = page
            .locator('button.cds--overflow-menu')
            .or(page.getByRole('button', {
            name: /options|context menu|open menu|more|actions/i,
        }))
            .first();
        this.editContextMenuOption = page
            .getByRole('menuitem', { name: /edit/i })
            .or(page.getByRole('button', { name: /edit/i }))
            .or(page.locator('button.cds--overflow-menu-options__btn', {
            hasText: /edit/i,
        }))
            .first();
        this.deleteContextMenuOption = page
            .getByRole('menuitem', { name: /delete/i })
            .or(page.getByRole('button', { name: /delete/i }))
            .or(page.locator('button.cds--overflow-menu-options__btn', {
            hasText: /delete/i,
        }))
            .first();
        this.confirmDeleteButton = page
            .locator('.cds--modal button.confirm')
            .or(page.locator('.Deleter button.confirm'))
            .or(page.getByRole('dialog').getByRole('button', { name: /delete/i }))
            .first();
    }
    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 });
    }
    async createCollection(name) {
        await this.clickCreateNewButton();
        await (0, test_1.expect)(this.collectionOption).toBeVisible({ timeout: 30000 });
        await this.collectionOption.click({ timeout: 30000 });
        await (0, test_1.expect)(this.collectionNameInput).toBeVisible({ timeout: 30000 });
        await this.collectionNameInput.click();
        await this.collectionNameInput.fill(name);
        await (0, test_1.expect)(this.createCollectionConfirmButton).toBeVisible({
            timeout: 30000,
        });
        await this.createCollectionConfirmButton.click({ timeout: 30000 });
        await this.finalizeCollectionCreationIfNeeded();
    }
    async finalizeCollectionCreationIfNeeded() {
        const addSourceModal = this.page.getByRole('dialog', {
            name: /add source/i,
        });
        if (await addSourceModal.isVisible({ timeout: 15000 }).catch(() => false)) {
            const createCollectionButton = addSourceModal.getByRole('button', {
                name: /^create collection$/i,
            });
            await (0, test_1.expect)(createCollectionButton).toBeVisible({ timeout: 30000 });
            await createCollectionButton.click({ timeout: 30000 });
        }
    }
    async assertCollectionVisible(name) {
        await (0, test_1.expect)(this.page
            .getByRole('heading', { name })
            .or(this.page.getByText(name))
            .first()).toBeVisible({ timeout: 60000 });
    }
    async renameCollection(newName) {
        await (0, test_1.expect)(this.collectionContextMenuButton).toBeVisible({
            timeout: 30000,
        });
        await this.collectionContextMenuButton.click({ timeout: 30000 });
        await (0, test_1.expect)(this.editContextMenuOption).toBeVisible({ timeout: 30000 });
        await this.editContextMenuOption.click({ timeout: 30000 });
        await (0, test_1.expect)(this.collectionNameInput).toBeVisible({ timeout: 30000 });
        await this.collectionNameInput.click();
        await this.collectionNameInput.fill(newName);
        await (0, test_1.expect)(this.createCollectionConfirmButton).toBeVisible({
            timeout: 30000,
        });
        await this.createCollectionConfirmButton.click({ timeout: 30000 });
    }
    async deleteCollection() {
        await (0, test_1.expect)(this.collectionContextMenuButton).toBeVisible({
            timeout: 30000,
        });
        await this.collectionContextMenuButton.click({ timeout: 30000 });
        await (0, test_1.expect)(this.deleteContextMenuOption).toBeVisible({ timeout: 30000 });
        await this.deleteContextMenuOption.click({ timeout: 30000 });
        await (0, test_1.expect)(this.confirmDeleteButton).toBeVisible({ timeout: 30000 });
        await this.confirmDeleteButton.click({ timeout: 30000 });
    }
    async assertCollectionNotVisible(name) {
        await (0, test_1.expect)(this.page.getByText(name).first()).toBeHidden({
            timeout: 60000,
        });
    }
}
exports.OptimizeCollectionsPage = OptimizeCollectionsPage;
