"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginPage = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("./UtilitiesPage");
const userNameInputValidInputSchema = {
    _schema: {
        maxlength: 254,
        minlength: 5,
        violationEffect: 'ARIA_INVALID_STATE',
    },
};
class LoginPage {
    page;
    usernameInput;
    passwordInput;
    continueButton;
    loginButton;
    loginMessage;
    errorMessage;
    passwordHeading;
    constructor(page) {
        this.page = page;
        this.usernameInput = {
            locator: page
                .getByLabel('Email address')
                .and(page.locator(':not([id="c4-invite-email"])')),
            schema: userNameInputValidInputSchema,
        };
        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.locator.fill(username);
    }
    async clickContinueButton() {
        await this.continueButton.click({ timeout: 30000 });
    }
    async fillPassword(password) {
        await this.passwordInput.waitFor({ state: 'visible', timeout: 120000 });
        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.locator).toBeVisible({ timeout: 200000 });
        await this.fillUsername(username);
        await this.clickContinueButton();
        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 loginWithTestUser(credentials = {}) {
        const { username = process.env.C8_USERNAME_TEST, password = process.env.C8_PASSWORD_TEST, } = credentials;
        // Navigate to app root to trigger auth redirect. The Console app (at
        // console.* hostnames) embeds a hidden invite-user email input also labeled
        // "Email address" that would otherwise satisfy the locator below before the
        // auth0 redirect has had a chance to complete.
        await this.page.goto('/');
        await this.page
            .waitForURL((url) => !url.hostname.includes('console'), { timeout: 60000 })
            .catch(() => { });
        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.locator).toBeVisible({ timeout: 120000 });
            await this.fillUsername(username);
            await this.clickContinueButton();
        }
        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.locator).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;
