"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const UtilitiesPage_1 = require("../8.10/UtilitiesPage");
class AppsPage {
    page;
    modelerLink;
    tasklistLink;
    adminLink;
    operateLink;
    optimizeButton;
    adminButton;
    tasklistButton;
    operateButton;
    operateListItem;
    adminListItem;
    optimizeListItem;
    tasklistListItem;
    appSwitcherButton;
    optimizeLink;
    consoleLink;
    camundaComponentsButton;
    camundaAppsButton;
    constructor(page) {
        this.page = page;
        this.modelerLink = page.getByRole('link', { name: 'Modeler', exact: true });
        this.appSwitcherButton = page.getByLabel('App Switcher');
        this.tasklistLink = page.getByRole('link', { name: 'Tasklist', exact: true });
        this.adminLink = page.getByRole('link', { name: /^(Admin|Identity)$/ });
        this.operateLink = page.getByRole('link', { name: 'Operate', exact: true });
        this.optimizeLink = page.getByRole('link', { name: 'Optimize', exact: true });
        this.consoleLink = page.getByRole('link', { name: 'Console', exact: true });
        this.camundaComponentsButton = page.getByLabel('Camunda components');
        this.camundaAppsButton = page.getByLabel('Camunda apps');
        this.tasklistListItem = this.page
            .getByRole('listitem')
            .filter({ hasText: 'Tasklist' });
        this.optimizeListItem = this.page
            .getByRole('listitem')
            .filter({ hasText: 'Optimize' });
        this.operateListItem = this.page
            .getByRole('listitem')
            .filter({ hasText: 'Operate' });
        this.adminListItem = this.page
            .getByRole('listitem')
            .filter({ hasText: /Admin|Identity/ }); // Identity Should be removed after https://github.com/camunda/camunda/issues/46447 is fixed
        this.operateButton = page.getByRole('button', {
            name: 'Operate',
            exact: true,
        });
        this.optimizeButton = page.getByRole('button', {
            name: 'Optimize',
            exact: true,
        });
        this.tasklistButton = page.getByRole('button', {
            name: 'Tasklist',
            exact: true,
        });
        this.adminButton = page.getByRole('button', {
            name: /^(Admin|Identity)$/, // Identity Should be removed after https://github.com/camunda/camunda/issues/46447 is fixed
        });
    }
    async clickCluster(component, name) {
        const cluster = component.getByRole('link', { name: name, exact: true });
        await (0, test_1.expect)(cluster).toBeVisible({ timeout: 30000 });
        await cluster.click();
    }
    async clickModeler() {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    await (0, test_1.expect)(this.modelerLink).toBeVisible({ timeout: 30000 });
                    await this.modelerLink.click();
                }
                else {
                    await this.clickCamundaApps();
                    await (0, test_1.expect)(this.modelerLink).toBeVisible({ timeout: 30000 });
                    await this.modelerLink.click();
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
            }
        }
        throw new Error(`Failed to click the modeler link after ${maxRetries} attempts.`);
    }
    async clickTasklist(clusterName) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    //Single cluster
                    if (await this.tasklistLink.isVisible({ timeout: 5000 })) {
                        await this.tasklistLink.click({ timeout: 10000 });
                    }
                    else {
                        //Multiple clusters
                        await this.doClickClusterInTasklist(clusterName);
                    }
                }
                else {
                    await this.clickCamundaApps();
                    if (await this.tasklistLink.isVisible({ timeout: 5000 })) {
                        await this.tasklistLink.click({ timeout: 10000 });
                    }
                    else {
                        await this.doClickClusterInTasklist(clusterName);
                    }
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                if (!this.page.isClosed()) {
                    await this.page
                        .waitForLoadState('load', { timeout: 20000 })
                        .catch(() => { });
                    await this.page.reload();
                    await (0, sleep_1.sleep)(20000);
                }
            }
        }
        throw new Error(`Failed to click the tasklist link after ${maxRetries} attempts.`);
    }
    async clickAdmin(clusterName) {
        const maxRetries = 5;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    //Single cluster
                    if (await this.adminLink.isVisible({ timeout: 5000 })) {
                        await this.adminLink.click({ timeout: 10000 });
                    }
                    else {
                        //Multiple clusters
                        await this.doClickClusterInAdmin(clusterName);
                    }
                }
                else {
                    await this.clickCamundaApps();
                    if (await this.adminLink.isVisible({ timeout: 5000 })) {
                        await this.adminLink.click({ timeout: 10000 });
                    }
                    else {
                        await this.doClickClusterInAdmin(clusterName);
                    }
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await (0, UtilitiesPage_1.waitForLoadingToFinish)(this.page);
                await this.page.reload();
                await (0, sleep_1.sleep)(20000);
            }
        }
        throw new Error(`Failed to click the Identity link after ${maxRetries} attempts.`);
    }
    async doClickClusterInOperate(clusterName) {
        if (await this.operateListItem
            .getByRole('button', {
            expanded: true,
        })
            .isHidden({ timeout: 5000 })) {
            await this.operateButton.click({ timeout: 10000 });
        }
        await this.clickCluster(this.operateListItem, clusterName);
    }
    async doClickClusterInTasklist(clusterName) {
        if (await this.tasklistListItem
            .getByRole('button', {
            expanded: true,
        })
            .isHidden({ timeout: 5000 })) {
            await this.tasklistButton.click({ timeout: 10000 });
        }
        await this.clickCluster(this.tasklistListItem, clusterName);
    }
    async doClickClusterInAdmin(clusterName) {
        if (await this.adminListItem
            .getByRole('button', {
            expanded: true,
        })
            .isHidden({ timeout: 5000 })) {
            await this.adminButton.click({ timeout: 10000 });
        }
        await this.clickCluster(this.adminListItem, clusterName);
    }
    async doClickClusterInOptimize(clusterName) {
        if (await this.optimizeListItem
            .getByRole('button', {
            expanded: true,
        })
            .isHidden({ timeout: 5000 })) {
            await this.optimizeButton.click({ timeout: 10000 });
        }
        await this.clickCluster(this.optimizeListItem, clusterName);
    }
    async clickOperate(clusterName) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    if (await this.operateLink.isVisible({ timeout: 5000 })) {
                        //Single Cluster
                        await this.operateLink.click({ timeout: 10000 });
                    }
                    else {
                        //Multiple Cluster
                        await this.doClickClusterInOperate(clusterName);
                    }
                }
                else {
                    await this.clickCamundaApps();
                    if (await this.operateLink.isVisible({ timeout: 5000 })) {
                        await this.operateLink.click({ timeout: 10000 });
                    }
                    else {
                        await this.doClickClusterInOperate(clusterName);
                    }
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await this.page.waitForLoadState('domcontentloaded');
                await this.page.reload();
                await (0, sleep_1.sleep)(20000);
            }
        }
        throw new Error(`Failed to click the operate link after ${maxRetries} attempts.`);
    }
    async clickOptimize(clusterName) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    if (await this.optimizeLink.isVisible({ timeout: 10000 })) {
                        //Single cluster
                        await this.optimizeLink.click({ timeout: 10000 });
                    }
                    else {
                        //Multiple clusters
                        await this.doClickClusterInOptimize(clusterName);
                    }
                }
                else {
                    await this.clickCamundaApps();
                    if (await this.optimizeLink.isVisible({ timeout: 5000 })) {
                        await this.optimizeLink.click({ timeout: 10000 });
                    }
                    else {
                        await this.doClickClusterInOptimize(clusterName);
                    }
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await this.page.waitForLoadState('domcontentloaded');
                await this.page.reload();
                await (0, sleep_1.sleep)(20000);
            }
        }
        throw new Error(`Failed to click the optimize link after ${maxRetries} attempts.`);
    }
    async clickCamundaApps(maxRetries = 3) {
        await (0, sleep_1.sleep)(2000);
        const appButtons = [
            this.camundaComponentsButton,
            this.camundaAppsButton,
            this.appSwitcherButton,
        ];
        for (let retries = 0; retries < maxRetries; retries++) {
            for (const appButton of appButtons) {
                if (await appButton.isVisible({ timeout: 20000 })) {
                    await appButton.click({ timeout: 30000 });
                    return;
                }
            }
        }
    }
    async clickConsoleLink() {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if (retries === 0) {
                    await (0, test_1.expect)(this.consoleLink).toBeVisible({ timeout: 30000 });
                    await this.consoleLink.click();
                }
                else {
                    await this.clickCamundaApps();
                    await (0, test_1.expect)(this.consoleLink).toBeVisible({ timeout: 30000 });
                    await this.consoleLink.click();
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
            }
        }
        throw new Error(`Failed to click the console link after ${maxRetries} attempts.`);
    }
    async assertOperatePresent(shouldBeVisible = true) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                const isVisible = await this.operateLink.isVisible({ timeout: 5000 });
                if (shouldBeVisible) {
                    if (isVisible) {
                        // Single Cluster
                        await (0, test_1.expect)(this.operateLink).toBeVisible();
                    }
                    else if (await this.operateListItem
                        .getByRole('button', { expanded: true })
                        .isHidden({ timeout: 5000 })) {
                        // Multiple Cluster
                        await (0, test_1.expect)(this.operateButton).toBeVisible();
                    }
                }
                else {
                    await (0, test_1.expect)(this.operateLink).not.toBeVisible();
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
            }
        }
        throw new Error(`Failed to assert the operate link visibility after ${maxRetries} attempts.`);
    }
    async assertTasklistPresent(shouldBeVisible = true) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                const isVisible = await this.tasklistLink.isVisible({ timeout: 5000 });
                if (shouldBeVisible) {
                    if (isVisible) {
                        // Single Cluster
                        await (0, test_1.expect)(this.tasklistLink).toBeVisible();
                    }
                    else if (await this.tasklistListItem
                        .getByRole('button', { expanded: true })
                        .isHidden({ timeout: 5000 })) {
                        // Multiple Cluster
                        await (0, test_1.expect)(this.tasklistButton).toBeVisible();
                    }
                }
                else {
                    await (0, test_1.expect)(this.tasklistLink).not.toBeVisible();
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
            }
        }
        throw new Error(`Failed to assert the tasklist link visibility after ${maxRetries} attempts.`);
    }
    async assertOptimizePresent(shouldBeVisible = true) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                const isVisible = await this.optimizeLink.isVisible({ timeout: 5000 });
                if (shouldBeVisible) {
                    if (isVisible) {
                        // Single Cluster
                        await (0, test_1.expect)(this.optimizeLink).toBeVisible();
                    }
                    else if (await this.optimizeListItem
                        .getByRole('button', { expanded: true })
                        .isHidden({ timeout: 5000 })) {
                        // Multiple Cluster
                        await (0, test_1.expect)(this.optimizeButton).toBeVisible();
                    }
                }
                else {
                    await (0, test_1.expect)(this.optimizeLink).not.toBeVisible();
                }
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
            }
        }
        throw new Error(`Failed to assert the optimize link visibility after ${maxRetries} attempts.`);
    }
}
exports.AppsPage = AppsPage;
