"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelerHomePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const clickLocatorWithRetry_1 = require("../../utils/assertionHelpers/clickLocatorWithRetry");
class ModelerHomePage {
    defaultFolderName = 'Cross Component Test Project';
    page;
    modelerPageBanner;
    createNewProjectButton;
    projectNameInput;
    formNameInput;
    chooseBpmnTemplateButton;
    diagramTypeDropdown;
    bpmnTemplateOption;
    formTemplateOption;
    projectBreadcrumb;
    openOrganizationsButton;
    manageButton;
    crossComponentProjectFolder;
    homeBreadcrumb;
    rows;
    uploadFilesButton;
    messageBanner;
    idpTemplateOption;
    idpApplicationNameInput;
    createIdpApplicationButton;
    dialog;
    closeButton;
    optionsButton;
    formNameBreadcrumb;
    constructor(page) {
        this.page = page;
        this.modelerPageBanner = page.getByRole('banner', {
            name: 'Camunda Modeler',
        });
        this.createNewProjectButton = page.getByRole('button', {
            name: 'New project',
        });
        this.projectNameInput = page.locator('[data-test="editable-input"]');
        this.formNameInput = page.locator('[data-test="editable-input"]');
        this.idpApplicationNameInput = page.getByPlaceholder('New IDP Application');
        this.createIdpApplicationButton = page.getByRole('button', {
            name: 'Create',
            exact: true,
        });
        this.chooseBpmnTemplateButton = page.getByRole('button', {
            name: 'Choose BPMN template',
        });
        this.idpTemplateOption = page
            .locator('[data-test="create-idp-application"]')
            .getByText('IDP Application');
        this.diagramTypeDropdown = page.locator('[data-test="diagram-dropdown"]');
        this.bpmnTemplateOption = page
            .locator('[data-test="create-bpmn-diagram"]')
            .getByText('BPMN Diagram');
        this.formTemplateOption = page
            .locator('[data-test="create-form"]')
            .getByText('Form');
        this.projectBreadcrumb = page.locator('[data-test="breadcrumb-project"]');
        this.homeBreadcrumb = page.locator('[data-test="breadcrumb-home"]');
        this.openOrganizationsButton = page.getByLabel('Open Organizations');
        this.manageButton = page.getByRole('menuitem', { name: 'Manage' });
        this.crossComponentProjectFolder = page
            .getByTitle(this.defaultFolderName, { exact: true })
            .first();
        this.rows = page.getByRole('row');
        this.uploadFilesButton = page.getByRole('menuitem', { name: 'Upload files' });
        this.messageBanner = page.locator('[data-test="close-top-banner"]');
        this.dialog = page.getByRole('dialog');
        this.closeButton = page.getByRole('button', { name: 'Got it - Dismiss' });
        this.optionsButton = page.getByRole('button', { name: 'Options' });
        this.formNameBreadcrumb = (formName) => page.locator('button[data-test="breadcrumb-form"]', {
            hasText: formName,
        });
    }
    async dismissOverlays() {
        for (const locator of [
            this.closeButton,
            this.page.getByRole('button', { name: 'Close' }).first(),
            this.messageBanner,
        ]) {
            await locator
                .waitFor({ state: 'visible', timeout: 3000 })
                .then(() => locator.click())
                .catch(() => { });
        }
    }
    async clickCreateNewProjectButton() {
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, this.createNewProjectButton, {
            preAction: async () => {
                await this.dismissOverlays();
            },
            totalTimeout: 120000,
            visibilityTimeout: 10000,
        });
    }
    async enterNewProjectName(name) {
        await this.projectNameInput.click({ timeout: 60000 });
        await this.projectNameInput.fill(name);
        await this.projectNameInput.press('Enter');
    }
    async enterIdpApplicationName(name) {
        await this.idpApplicationNameInput.click({ timeout: 60000 });
        await this.idpApplicationNameInput.fill(name);
    }
    async clickChooseBpmnTemplateButton() {
        await this.chooseBpmnTemplateButton.click();
    }
    async reload() {
        await this.page.reload();
    }
    async getProjectNames(name) {
        return this.rows
            .filter({ hasText: name })
            .getByTitle(name)
            .evaluateAll((elements) => elements
            .map((el) => el.textContent || '')
            .filter((text) => text !== ''));
    }
    async clickProcessDiagram(name) {
        const process = this.rows.filter({ hasText: name }).getByTitle(name).first();
        await (0, test_1.expect)(process).toBeVisible({ timeout: 90000 });
        await process.click();
    }
    async clickDiagramTypeDropdown() {
        await (0, test_1.expect)(this.diagramTypeDropdown).toBeVisible({
            timeout: 15000,
        });
        await this.diagramTypeDropdown.click();
    }
    async clickBpmnTemplateOption() {
        await (0, test_1.expect)(this.bpmnTemplateOption).toBeVisible({
            timeout: 15000,
        });
        await this.bpmnTemplateOption.click();
    }
    async clickFormOption() {
        await this.formTemplateOption.click();
    }
    async enterFormName(name) {
        await this.formNameInput.click({ timeout: 60000 });
        await this.formNameInput.fill(name);
        await this.formNameInput.press('Enter');
    }
    async clickProjectBreadcrumb() {
        await this.projectBreadcrumb.click({ timeout: 60000 });
    }
    async clickHomeBreadcrumb() {
        try {
            await this.homeBreadcrumb.click();
        }
        catch (error) {
            await this.page.getByText('Home', { exact: true }).first().click();
        }
    }
    async clickOpenOrganizationsButton() {
        await (0, sleep_1.sleep)(5000);
        await (0, test_1.expect)(this.openOrganizationsButton).toBeVisible({
            timeout: 10000,
        });
        await this.openOrganizationsButton.click({ timeout: 30000 });
    }
    async createForm(formName) {
        await this.clickDiagramTypeDropdown();
        await this.clickFormOption();
        await this.enterFormName(formName);
        await (0, sleep_1.sleep)(10000);
    }
    async clickCreateIdpApplicationButton() {
        await this.createIdpApplicationButton.click();
    }
    async clickCrossComponentProjectFolder() {
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, this.crossComponentProjectFolder, {
            visibilityTimeout: 60000,
            totalTimeout: 180000,
            maxRetries: 5,
            preAction: async () => {
                // A Carbon modal (`.cds--modal.is-visible`) sometimes covers the
                // project folder and intercepts pointer events; clickMessageBanner
                // only handles a single banner/close button so it doesn't dismiss
                // the modal. Escape it explicitly first — same pattern 8.7 already
                // uses for the same symptom.
                try {
                    const blockingModal = this.page.locator('.cds--modal.is-visible');
                    if ((await blockingModal.count()) > 0) {
                        await this.page.keyboard.press('Escape');
                        await (0, sleep_1.sleep)(1000);
                    }
                }
                catch {
                    // No blocking modal to dismiss
                }
                await this.clickMessageBanner();
            },
            postAction: async () => {
                await this.page.reload();
                await this.page.waitForLoadState('domcontentloaded');
            },
        });
    }
    async createCrossComponentProjectFolder() {
        await (0, sleep_1.sleep)(15000);
        await this.dismissOverlays();
        if (await this.crossComponentProjectFolder.isVisible()) {
            console.log('Cross Component Project folder already exists. Clicking into it');
            await this.clickCrossComponentProjectFolder();
            return;
        }
        await this.clickCreateNewProjectButton();
        await this.enterNewProjectName(this.defaultFolderName);
    }
    async clickManageButton(retries = 3) {
        await this.optionsButton.click({ timeout: 30000 });
        for (let i = 0; i < retries; i++) {
            try {
                await (0, sleep_1.sleep)(5000);
                if (await this.manageButton.isVisible({ timeout: 30000 })) {
                    await this.manageButton.click({ timeout: 10000 });
                }
                else {
                    await this.clickOpenOrganizationsButton();
                    await this.optionsButton.click({ timeout: 30000 });
                    await (0, test_1.expect)(this.manageButton).toBeVisible({ timeout: 30000 });
                    await this.manageButton.click({ timeout: 30000 });
                }
                return;
            }
            catch (error) {
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Failed to click manage button after ${retries} retries`);
    }
    async clickUploadFilesButton() {
        await this.uploadFilesButton.click({ timeout: 60000 });
    }
    async clickMessageBanner() {
        try {
            const button = this.messageBanner.or(this.closeButton).first();
            await (0, test_1.expect)(button).toBeVisible({ timeout: 15000 });
            await button.click();
        }
        catch {
            console.log('No banner or close button found to click');
        }
    }
    async selectCluster(clusterName) {
        //Select the correct cluster if multiple exist
        const baseClusterName = clusterName.split('(')[0].trim();
        const cluster = this.dialog.getByText(baseClusterName, { exact: false });
        await (0, test_1.expect)(cluster).toBeVisible({
            timeout: 30000,
        });
        await cluster.click();
        await (0, test_1.expect)(this.dialog
            .locator('label')
            .filter({
            hasText: clusterName,
        })
            .getByRole('switch')).toBeChecked({ timeout: 30000 });
    }
    async clickIdpApplicationTemplateOption() {
        await this.idpTemplateOption.click({ timeout: 120000 });
    }
    async assertFormBreadcrumbVisible(formName) {
        await (0, test_1.expect)(this.formNameBreadcrumb(formName)).toBeVisible({
            timeout: 120000,
        });
    }
}
exports.ModelerHomePage = ModelerHomePage;
