"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelerHomePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
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;
    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);
        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' });
    }
    async clickCreateNewProjectButton() {
        await this.createNewProjectButton.click({ timeout: 60000 });
    }
    async enterNewProjectName(name) {
        await this.projectNameInput.click({ timeout: 60000 });
        await this.projectNameInput.fill(name);
        await this.projectNameInput.press('Enter');
    }
    async createCrossComponentProjectFolder() {
        await (0, sleep_1.sleep)(10000);
        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, sleep_1.sleep)(5000);
    }
    async clickCrossComponentProjectFolder() {
        let attempts = 0;
        const maxAttempts = 3;
        const timeout = 60000;
        while (attempts < maxAttempts) {
            try {
                await (0, test_1.expect)(this.crossComponentProjectFolder).toBeVisible({ timeout });
                await this.crossComponentProjectFolder.click();
                return;
            }
            catch (error) {
                attempts++;
                if (attempts < maxAttempts) {
                    console.log(`Attempt ${attempts} failed. Reloading page and retrying...`);
                    await this.page.reload();
                    await (0, sleep_1.sleep)(10000);
                }
                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, test_1.expect)(this.diagramTypeDropdown).toBeVisible({
            timeout: 60000,
        });
        await this.diagramTypeDropdown.click();
    }
    async clickBpmnTemplateOption() {
        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();
    }
    async clickOpenOrganizationsButton() {
        await this.openOrganizationsButton.click({ timeout: 30000 });
    }
    async clickManageButton() {
        await this.manageButton.click({ timeout: 30000 });
    }
    async clickUploadFilesButton() {
        await this.uploadFilesButton.click({ timeout: 60000 });
    }
}
exports.ModelerHomePage = ModelerHomePage;
