"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginPage = void 0;
class LoginPage {
    page;
    usernameInput;
    passwordInput;
    loginButton;
    coreUsernameInput;
    corePasswordInput;
    coreLoginButton;
    keycloakBanner;
    useCore = false;
    useKeycloak = false;
    constructor(page) {
        this.page = page;
        this.usernameInput = page.getByLabel('Username or email');
        this.passwordInput = page.getByLabel('Password');
        this.loginButton = page.getByRole('button', { name: /log in|sign in/i });
        this.coreUsernameInput = page.getByPlaceholder('Username');
        this.corePasswordInput = page.getByPlaceholder('Password');
        this.coreLoginButton = page.getByRole('button', { name: 'Login' });
        this.keycloakBanner = page.locator('body#keycloak-bg[data-page-id="login-login"]');
    }
    async detectLoginForm() {
        // If we're on an app page that's about to redirect to KC, wait for
        // the navigation to complete before looking for login form elements.
        const url = this.page.url();
        if (!url.includes('/auth/') && !url.includes('/login-callback')) {
            await this.page
                .waitForURL((u) => u.pathname.includes('/auth/'), {
                timeout: 30000,
                waitUntil: 'domcontentloaded',
            })
                .catch(() => { });
        }
        const kcUsername = this.page.locator('#username');
        const anyForm = kcUsername
            .or(this.coreLoginButton)
            .or(this.keycloakBanner)
            .first();
        await anyForm.waitFor({ state: 'visible', timeout: 30000 });
        if (await this.coreLoginButton.isVisible()) {
            this.useCore = true;
        }
        else if (await this.keycloakBanner.isVisible()) {
            this.useKeycloak = true;
        }
    }
    async fillUsername(username) {
        if (await this.page.locator('#username').isVisible()) {
            await this.page.fill('#username', username);
        }
        else {
            await (this.useCore
                ? this.coreUsernameInput.fill(username)
                : this.usernameInput.fill(username));
        }
    }
    async clickUsername() {
        if (await this.page.locator('#username').isVisible()) {
            await this.page.click('#username');
        }
        else {
            await (this.useCore
                ? this.coreUsernameInput.click({ timeout: 60000 })
                : this.usernameInput.click({ timeout: 60000 }));
        }
    }
    async fillPassword(password) {
        if (await this.page.locator('#password').isVisible()) {
            await this.page.fill('#password', password);
        }
        else {
            await (this.useCore
                ? this.corePasswordInput.fill(password)
                : this.passwordInput.fill(password));
        }
    }
    async clickLoginButton() {
        if (await this.page.locator('button[type="submit"]').isVisible()) {
            await this.page.click('button[type="submit"]');
        }
        else {
            await (this.useCore
                ? this.coreLoginButton.click({ timeout: 60000 })
                : this.loginButton.click({ timeout: 60000 }));
        }
    }
    async login(username, password) {
        try {
            await this.detectLoginForm();
        }
        catch (error) {
            return;
        }
        await this.clickUsername();
        await this.fillUsername(username);
        await this.fillPassword(password);
        await this.clickLoginButton();
    }
}
exports.LoginPage = LoginPage;
