"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginPage = void 0;
const test_1 = require("@playwright/test");
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' });
        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"]');
    }
    // Race logic: call before interacting
    async detectLoginForm() {
        const winner = await Promise.race([
            this.usernameInput.waitFor({ state: 'visible' }).then(() => 'default'),
            this.coreUsernameInput.waitFor({ state: 'visible' }).then(() => 'core'),
            this.keycloakBanner.waitFor({ state: 'visible' }).then(() => 'keycloak'),
        ]);
        this.useCore = winner === 'core';
        this.useKeycloak = winner === 'keycloak';
    }
    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();
            if (await this.page
                .locator('body#keycloak-bg[data-page-id="login-login"]')
                .isVisible()) {
                await (0, test_1.expect)(this.keycloakBanner).toBeVisible({ timeout: 15000 });
            }
            else {
                const activeLoginButton = this.useCore
                    ? this.coreLoginButton
                    : this.loginButton;
                await (0, test_1.expect)(activeLoginButton).toBeVisible({ timeout: 15000 });
            }
        }
        catch (error) {
            return;
        }
        await this.clickUsername();
        await this.fillUsername(username);
        await this.fillPassword(password);
        await this.clickLoginButton();
    }
    async isLoginPage() {
        try {
            await Promise.all([
                this.usernameInput.waitFor({ state: 'visible', timeout: 10000 }),
                this.loginButton.waitFor({ state: 'visible', timeout: 10000 }),
            ]);
            return true;
        }
        catch {
            return false;
        }
    }
}
exports.LoginPage = LoginPage;
