"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;
    rateLimitError;
    constructor(page) {
        this.page = page;
        this.usernameInput = {
            locator: page
                .getByLabel(/Email address/i)
                .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');
        this.rateLimitError = page
            .getByText(/rate limit has been reached/i)
            .or(page.getByText(/We are sorry, an error occurred/i));
    }
    async fillUsername(username) {
        await (0, test_1.expect)(this.usernameInput.locator).toBeVisible({ timeout: 120000 });
        await this.usernameInput.locator.fill(username);
    }
    async clickContinueButton() {
        await (0, test_1.expect)(this.continueButton).toBeEnabled({ timeout: 30000 });
        await this.continueButton.click({ timeout: 30000 });
    }
    async fillPassword(password) {
        await (0, test_1.expect)(this.passwordInput).toBeVisible({ 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.loginMessage).toBeVisible({ timeout: 120000 });
        await (0, test_1.expect)(this.usernameInput.locator).toBeVisible({ timeout: 30000 });
        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(() => { });
        let authStep = 'unknown';
        const deadline = Date.now() + 120000;
        while (Date.now() < deadline) {
            const [passwordHeadingVisible, passwordInputVisible, usernameVisible, rateLimitVisible,] = await Promise.all([
                this.passwordHeading.isVisible().catch(() => false),
                this.passwordInput.isVisible().catch(() => false),
                this.usernameInput.locator.isVisible().catch(() => false),
                this.rateLimitError
                    .first()
                    .isVisible()
                    .catch(() => false),
            ]);
            if (rateLimitVisible) {
                throw new Error('AUTH0_RATE_LIMIT: identity provider returned an error page');
            }
            if (passwordHeadingVisible || passwordInputVisible) {
                authStep = 'password';
                break;
            }
            if (usernameVisible) {
                authStep = 'username';
                break;
            }
            await this.page.waitForTimeout(1000);
        }
        await (0, test_1.expect)(authStep).not.toBe('unknown');
        if (authStep === 'username') {
            await (0, test_1.expect)(this.usernameInput.locator).toBeVisible({ timeout: 120000 });
            await this.fillUsername(username);
            await this.clickContinueButton();
            await (0, test_1.expect)(this.passwordInput).toBeVisible({ timeout: 120000 });
        }
        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;
