"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NavigationPage = void 0;
const test_1 = require("@playwright/test");
const LoginPage_1 = require("./LoginPage");
const sleep_1 = require("../../utils/sleep");
const constants_1 = require("../../utils/constants");
class NavigationPage {
    page;
    modelerPageBanner;
    operatePageBanner;
    tasklistPageBanner;
    optimizePageBanner;
    identityPageBanner;
    consolePageBanner;
    keyboardPageBanner;
    constructor(page) {
        this.page = page;
        this.operatePageBanner = page.getByRole('link', {
            name: 'Camunda logo Operate',
        });
        this.modelerPageBanner = page
            .locator('a')
            .filter({ hasText: 'Camunda logoModeler' });
        this.tasklistPageBanner = page.getByRole('link', {
            name: 'Camunda logo Tasklist',
        });
        this.optimizePageBanner = page.getByRole('link', {
            name: 'Camunda logo Optimize',
        });
        this.identityPageBanner = page.getByRole('link', {
            name: 'Camunda logo Identity',
        });
        this.consolePageBanner = page.getByRole('link', {
            name: 'Camunda logo Console',
        });
        this.keyboardPageBanner = page.locator('#keycloak-bg');
    }
    async goTo(url, banner, sleepTimeout, { username = 'demo', password = process.env.DISTRO_QA_E2E_TESTS_IDENTITY_FIRSTUSER_PASSWORD ??
        'demo', } = {}, maxRetries = 3) {
        const startTime = Date.now();
        let timeout = constants_1._1_SECOND_IN_MS * 1;
        if (url === '/modeler') {
            timeout = constants_1._1_SECOND_IN_MS * 10;
        }
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            try {
                if (sleepTimeout) {
                    await (0, sleep_1.sleep)(sleepTimeout);
                }
                await this.page.goto(url, {
                    timeout: 60000,
                    waitUntil: 'domcontentloaded',
                });
                const loginPage = new LoginPage_1.LoginPage(this.page);
                const loginSelector = loginPage.usernameInput;
                await (0, test_1.expect)(banner.or(loginSelector)).toBeVisible({ timeout: 60000 });
                const isBannerVisible = await banner.isVisible();
                if (isBannerVisible) {
                    try {
                        await (0, test_1.expect)(loginSelector).toBeVisible({ timeout: 5000 });
                        await loginPage.login(username, password);
                    }
                    catch (error) {
                        // The login selector did NOT appear. The banner is real. We are on the correct page.
                        // The navigation was successful, so we can just return.
                    }
                }
                else {
                    await loginPage.login(username, password);
                }
                return;
            }
            catch (error) {
                const now = new Date();
                const elapsed = Date.now() - startTime;
                if (attempt < maxRetries - 1) {
                    await (0, sleep_1.sleep)(5000);
                    if (!this.page.isClosed()) {
                        try {
                            await this.page.waitForLoadState('load', { timeout: timeout });
                        }
                        catch (error) {
                            const now2 = new Date();
                            const elapsed2 = Date.now() - startTime;
                            console.warn(`[${now2.toISOString()}] (+${elapsed2}ms) [NavigationPage] Load state timeout or error: ${error}`);
                        }
                    }
                }
                else {
                    console.error(`[${now.toISOString()}] (+${elapsed}ms) [NavigationPage] Login (${url}) failed after ${maxRetries} attempts`);
                    throw new Error(`Login (${url}) failed after ${maxRetries} attempts`);
                }
            }
        }
    }
    async goToModeler(sleepTimeout, credentials) {
        await this.goTo('/modeler', this.modelerPageBanner, sleepTimeout, credentials);
    }
    async goToTasklist(sleepTimeout, credentials) {
        await this.goTo('/tasklist', this.tasklistPageBanner, sleepTimeout, credentials);
    }
    async goToOperate(sleepTimeout, credentials) {
        await this.goTo('/operate', this.operatePageBanner, sleepTimeout, credentials);
    }
    async goToOptimize(sleepTimeout, credentials) {
        await this.goTo('/optimize', this.optimizePageBanner, sleepTimeout, credentials);
    }
    async goToKeycloak() {
        await this.page.goto('/auth');
    }
    async goToIdentity(sleepTimeout, credentials) {
        await this.goTo('/identity', this.identityPageBanner, sleepTimeout, credentials);
    }
    async goToConsole(sleepTimeout, credentials) {
        await this.goTo('/console', this.consolePageBanner, sleepTimeout, credentials);
    }
}
exports.NavigationPage = NavigationPage;
