"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelerHomePage = void 0;
const test_1 = require("@playwright/test");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
class ModelerHomePage {
    page;
    defaultFolderName = 'Cross Component Test Project';
    modelerPageBanner;
    createNewProjectButton;
    projectNameInput;
    chooseBpmnTemplateButton;
    diagramTypeDropdown;
    bpmnTemplateOption;
    formTemplateOption;
    projectBreadcrumb;
    openOrganizationsButton;
    manageButton;
    crossComponentProjectFolder;
    homeBreadcrumb;
    formNameInput;
    rows;
    licenseKeyTagNonProduction;
    licenseKeyTagNonCommercial;
    licenseKeyTagProduction;
    licenseKeyTagCommercial;
    uploadFilesButton;
    messageBanner;
    closeButton;
    modalCloseButton;
    constructor(page) {
        this.page = page;
        this.modelerPageBanner = page
            .locator('a')
            .filter({ hasText: 'Camunda logoModeler' });
        this.createNewProjectButton = page.getByRole('button', {
            name: 'New project',
        });
        this.projectNameInput = page.locator('[data-test="editable-input"]');
        this.chooseBpmnTemplateButton = page.getByRole('button', {
            name: 'Choose BPMN template',
        });
        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.openOrganizationsButton = page.getByLabel('Open Organizations');
        this.manageButton = page.getByRole('button', { name: 'Manage' });
        this.crossComponentProjectFolder = page
            .getByTitle(this.defaultFolderName, {
            exact: true,
        })
            .first();
        this.homeBreadcrumb = page.locator('[data-test="breadcrumb-home"]');
        this.formNameInput = page.locator('[data-test="editable-input"]');
        this.rows = page.getByRole('row');
        this.licenseKeyTagNonProduction = page
            .getByText('Non-production license')
            .first();
        this.licenseKeyTagNonCommercial = page.getByText('Non-commercial license');
        this.licenseKeyTagProduction = page.getByText('Production license').first();
        this.licenseKeyTagCommercial = page.getByText('Commercial license');
        this.uploadFilesButton = page.getByRole('menuitem', { name: 'Upload files' });
        this.messageBanner = page.locator('[data-test="close-top-banner"]');
        this.closeButton = page.getByRole('button', { name: 'Got it - Dismiss' });
        this.modalCloseButton = page.locator('.cds--modal.is-visible .cds--modal-close');
    }
    async clickCreateNewProjectButton() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.createNewProjectButton, {
            totalTimeout: 30000,
        });
        // Modals (WhatsNew, "BPMN conditional events", etc.) render lazily after
        // page load and intercept clicks on the button underneath. Dismiss them.
        await this.closeButton
            .waitFor({ state: 'visible', timeout: 5000 })
            .then(() => this.closeButton.click())
            .catch(() => { });
        await this.modalCloseButton
            .waitFor({ state: 'visible', timeout: 2000 })
            .then(() => this.modalCloseButton.click())
            .catch(() => { });
        await this.createNewProjectButton.click();
    }
    async enterNewProjectName(name) {
        await (0, test_1.expect)(this.projectNameInput).toBeVisible({ timeout: 10000 });
        await this.projectNameInput.click({ timeout: 60000 });
        await this.projectNameInput.fill(name);
        await this.projectNameInput.press('Enter');
    }
    async createCrossComponentProjectFolder() {
        // Removed waitForLoadState('networkidle'). Modeler emits
        // background activity; the toBeVisible auto-wait covers
        // readiness deterministically.
        await (0, test_1.expect)(this.createNewProjectButton).toBeVisible({ timeout: 30000 });
        await this.clickMessageBanner();
        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);
        await (0, test_1.expect)(this.page.getByRole('heading', { name: this.defaultFolderName })).toBeVisible({ timeout: 10000 });
    }
    async clickCrossComponentProjectFolder() {
        let attempts = 0;
        const maxAttempts = 3;
        const timeout = 60000;
        while (attempts < maxAttempts) {
            try {
                await this.clickMessageBanner();
                await (0, test_1.expect)(this.crossComponentProjectFolder).toBeVisible({ timeout });
                // The WhatsNew modal renders lazily after page load and intercepts clicks.
                // Explicitly wait for it to appear (up to 5s), dismiss it, then click.
                await this.closeButton
                    .waitFor({ state: 'visible', timeout: 5000 })
                    .then(() => this.closeButton.click())
                    .catch(() => { });
                await this.modalCloseButton
                    .waitFor({ state: 'visible', timeout: 2000 })
                    .then(() => this.modalCloseButton.click())
                    .catch(() => { });
                await this.crossComponentProjectFolder.click();
                await this.clickMessageBanner();
                return;
            }
            catch (error) {
                attempts++;
                if (attempts < maxAttempts) {
                    console.log(`Attempt ${attempts} failed. Reloading page and retrying...`);
                    await this.page.reload();
                    await this.page.waitForLoadState('networkidle');
                }
                else {
                    throw new Error('Failed to click crossComponentProjectFolder after 3 attempts.');
                }
            }
        }
    }
    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 clickHomeBreadcrumb() {
        try {
            await this.homeBreadcrumb.click();
        }
        catch (error) {
            await this.page.getByText('Home', { exact: true }).first().click();
        }
    }
    async clickChooseBpmnTemplateButton() {
        await this.chooseBpmnTemplateButton.click();
    }
    async clickDiagramTypeDropdown() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.diagramTypeDropdown, {
            postAction: async () => {
                await this.page.reload();
            },
        });
        await this.diagramTypeDropdown.click();
    }
    async clickBpmnTemplateOption() {
        await (0, test_1.expect)(this.bpmnTemplateOption).toBeVisible({ timeout: 60000 });
        await this.bpmnTemplateOption.click({ timeout: 60000 });
    }
    async clickFormOption() {
        await this.formTemplateOption.click();
    }
    async enterFormName(name) {
        await (0, test_1.expect)(this.formNameInput).toBeVisible({ timeout: 10000 });
        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 clickOpenOrganizationsButton() {
        await this.openOrganizationsButton.click({ timeout: 30000 });
    }
    async clickManageButton() {
        await this.manageButton.click({ timeout: 30000 });
    }
    async clickUploadFilesButton() {
        await this.uploadFilesButton.click({ timeout: 60000 });
    }
    async clickMessageBanner() {
        try {
            const banner = this.messageBanner
                .or(this.closeButton)
                .or(this.modalCloseButton);
            await banner.waitFor({ state: 'visible', timeout: 5000 });
            await banner.click();
        }
        catch {
            console.log('No banner or close button found to click');
        }
    }
}
exports.ModelerHomePage = ModelerHomePage;
