"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginPage = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("./UtilitiesPage");
class LoginPage {
    page;
    usernameInput;
    passwordInput;
    continueButton;
    loginButton;
    loginMessage;
    errorMessage;
    passwordHeading;
    constructor(page) {
        this.page = page;
        // The Console app embeds a hidden invite-user email input also labeled
        // "Email address" (id="c4-invite-email"). Exclude it so the locator only
        // resolves to the auth0 email input, regardless of redirect timing.
        this.usernameInput = page
            .getByLabel('Email address')
            .and(page.locator(':not([id="c4-invite-email"])'));
        this.passwordInput = page
            .getByLabel(/^Password\s*\*?$/i)
            .or(page.locator('input[type="password"]'))
            .first();
        this.continueButton = page.getByRole('button', {
            name: 'Continue',
            exact: true,
        });
        this.loginMessage = page.getByText('Log in to continue to Camunda.');
        this.passwordHeading = page.getByRole('heading', {
            name: 'Enter Your Password',
        });
        this.loginButton = page.getByRole('button', {
            name: 'Continue',
            exact: true,
        });
        this.errorMessage = page.getByText('Wrong email or password');
    }
    async fillUsername(username) {
        await this.usernameInput.fill(username);
    }
    async clickContinueButton() {
        await this.continueButton.click();
    }
    async fillPassword(password) {
        await this.passwordInput.fill(password);
    }
    async clickLoginButton() {
        await this.loginButton.click({ timeout: 60000 });
    }
    async login(credentials = {}) {
        const { username = process.env.C8_USERNAME, password = process.env.C8_PASSWORD, } = credentials;
        await (0, test_1.expect)(this.usernameInput).toBeVisible({ timeout: 60000 });
        await this.fillUsername(username);
        await this.clickContinueButton();
        await (0, test_1.expect)(this.passwordInput).toBeVisible({ timeout: 60000 });
        await this.fillPassword(password);
        await (0, test_1.expect)(this.loginButton).toBeVisible({ timeout: 60000 });
        await this.clickLoginButton();
        await (0, UtilitiesPage_1.assertTestUsesCorrectOrganization)(this.page);
    }
    async loginWithTestUser(credentials = {}) {
        const { username = process.env.C8_USERNAME_TEST, password = process.env.C8_PASSWORD_TEST, } = credentials;
        // Navigate to app root to start the auth flow.
        await this.page.goto('/');
        await this.page
            .waitForURL((url) => !url.hostname.includes('console'), { timeout: 60000 })
            .catch(() => { });
        // SSO re-auth may silently log us back in as the previous user after
        // logout/session-reset, keeping the page on Console instead of redirecting
        // to the Auth0 login form. Detect Console (settings button visible) and
        // force logout until the Auth0 email input appears.
        const settingsBtn = this.page.getByLabel('Open Settings');
        const logoutBtn = this.page.getByRole('button', { name: 'Log out' });
        for (let i = 0; i < 3; i++) {
            const onAuth0 = await this.usernameInput
                .waitFor({ state: 'visible', timeout: 15000 })
                .then(() => true)
                .catch(() => false);
            if (onAuth0)
                break;
            if (await settingsBtn.isVisible({ timeout: 3000 })) {
                await settingsBtn.click({ timeout: 30000 });
                await logoutBtn.click({ timeout: 30000 });
                await this.page.waitForLoadState('domcontentloaded', { timeout: 30000 });
            }
        }
        const onPasswordStep = (await this.passwordHeading
            .isVisible({ timeout: 5000 })
            .catch(() => false)) ||
            (await this.passwordInput.isVisible({ timeout: 5000 }).catch(() => false));
        if (!onPasswordStep) {
            await (0, test_1.expect)(this.usernameInput).toBeVisible({ timeout: 120000 });
            await this.fillUsername(username);
            await this.clickContinueButton();
        }
        await (0, test_1.expect)(this.passwordInput).toBeVisible({ timeout: 180000 });
        await this.fillPassword(password);
        await (0, test_1.expect)(this.loginButton).toBeVisible({ timeout: 120000 });
        await this.clickLoginButton();
        await (0, UtilitiesPage_1.assertTestUsesCorrectOrganization)(this.page);
    }
    async loginFailAssertion() {
        try {
            await (0, test_1.expect)(this.errorMessage).toContainText('Wrong email or password');
        }
        catch (error) {
            await (0, test_1.expect)(this.page.getByText('Your account has been blocked after multiple consecutive login attempts')).toBeVisible({ timeout: 20000 });
        }
    }
    async loginWithoutOrgAssertion(credentials = {}) {
        const { username = process.env.C8_USERNAME, password = process.env.C8_PASSWORD, } = credentials;
        await (0, test_1.expect)(this.usernameInput).toBeVisible({ timeout: 180000 });
        await this.fillUsername(username);
        await this.clickContinueButton();
        await this.fillPassword(password);
        await (0, test_1.expect)(this.loginButton).toBeVisible({ timeout: 120000 });
        await this.clickLoginButton();
    }
}
exports.LoginPage = LoginPage;
