"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HomePage = void 0;
const test_1 = require("@playwright/test");
class HomePage {
    page;
    dashboardTab;
    clusterTab;
    organizationTab;
    organizationViewTitle;
    welcomeMessage;
    informationDialog;
    camundaAppsButton;
    camundaComponentsButton;
    appSwitcher;
    openOrganizationButton;
    consoleBanner;
    modelerBanner;
    gettingStartedHeading;
    buttonSkipCustomization;
    constructor(page) {
        this.page = page;
        this.dashboardTab = page.getByRole('link', { name: 'Dashboard' });
        this.clusterTab = page
            .getByRole('banner')
            .getByRole('link', { name: 'Clusters' });
        this.organizationTab = page.getByRole('link', { name: 'Organization' });
        this.organizationViewTitle = page.getByText('Organization Management');
        this.welcomeMessage = page.getByText('Welcome to Camunda!').first();
        this.informationDialog = page.getByRole('button', {
            name: 'Close',
        });
        this.camundaAppsButton = page.getByLabel('Camunda apps');
        this.appSwitcher = page.getByLabel('App Switcher');
        this.camundaComponentsButton = page
            .getByLabel('Camunda components')
            .or(this.camundaAppsButton)
            .or(this.appSwitcher);
        this.openOrganizationButton = page.getByLabel('Open Organizations');
        this.consoleBanner = page.getByRole('link', {
            name: 'Camunda logo Console',
        });
        this.gettingStartedHeading = page.getByText('start by creating a project');
        this.buttonSkipCustomization = page.getByRole('button', {
            name: 'Skip customization',
        });
        this.modelerBanner = page.getByRole('banner', {
            name: 'Camunda Modeler',
        });
    }
    async clickClusters() {
        await (0, test_1.expect)(this.clusterTab).toBeVisible({ timeout: 15000 });
        await this.clusterTab.click({ timeout: 15000 });
    }
    async clickDashboard() {
        await this.dashboardTab.click({ timeout: 90000 });
    }
    async clickOrganization() {
        await this.organizationTab.click({ timeout: 90000 });
    }
    async closeInformationDialog() {
        // Console can render more than one informational popup at once (e.g., the
        // customization modal plus a "New features" announcement). Close every visible
        // 'Close' button. Bounded to guard against a regression that re-renders the popup.
        //
        // Carbon stacks modals in DOM order, so .last() is the topmost; targeting
        // .first() would try to click a Close button whose modal is covered by the
        // next-stacked .cds--modal backdrop, which intercepts pointer events.
        // force: true also clicks through any transient overlay (e.g. a backdrop
        // fading in during animation).
        for (let i = 0; i < 5; i++) {
            const next = this.informationDialog.last();
            const visibleTimeout = i === 0 ? 30000 : 2000;
            if (!(await next.isVisible({ timeout: visibleTimeout }))) {
                return;
            }
            await next.click({ timeout: 30000, force: true });
        }
    }
    organizationUuid() {
        const uuidPattern = /\/org\/([a-f0-9-]+)/;
        const originalMatch = this.page.url().match(uuidPattern);
        if (!originalMatch || originalMatch.length != 2) {
            throw new Error('Organization UUID not found in the URL');
        }
        return originalMatch[1];
    }
    async clickOpenOrganizationButton() {
        await this.openOrganizationButton.click({ timeout: 60000 });
    }
    async clickSkipCustomization() {
        try {
            await (0, test_1.expect)(this.gettingStartedHeading).toBeVisible({
                timeout: 20000,
            });
            await (0, test_1.expect)(this.buttonSkipCustomization).toBeVisible({
                timeout: 20000,
            });
            await this.buttonSkipCustomization.click();
            await this.closeInformationDialog();
            return;
        }
        catch (error) {
            console.error(error);
        }
    }
}
exports.HomePage = HomePage;
