"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeycloakAdminPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class KeycloakAdminPage {
    page;
    keycloakBanner;
    credentialsTab;
    noCredentialsEmptyAction;
    passwordField;
    passwordConfirmationField;
    constructor(page) {
        this.page = page;
        this.keycloakBanner = page.locator('#kc-main-content-page-container');
        this.credentialsTab = page.getByTestId('credentials');
        this.noCredentialsEmptyAction = page.getByTestId('no-credentials-empty-action');
        this.passwordField = page.getByTestId('passwordField');
        this.passwordConfirmationField = page.getByTestId('passwordConfirmationField');
    }
    async toBeVisible() {
        await (0, test_1.expect)(this.keycloakBanner).toBeVisible({ timeout: 60000 });
    }
    async switchToCamundaPlatform() {
        const maxRetries = 3;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                const realmSelector = this.page.getByTestId('nav-item-realms');
                await realmSelector.click();
                const searchField = this.page.getByTestId('selectRealminput');
                await (0, test_1.expect)(searchField).toBeVisible({ timeout: 30000 });
                await searchField.click();
                await this.page
                    .getByPlaceholder('Search')
                    .fill(process.env.KEYCLOAK_REALM || 'camunda-platform');
                await this.page
                    .locator('[data-ouia-component-id="OUIA-Generated-Button-control-1"]')
                    .click();
                await this.page
                    .getByText(process.env.KEYCLOAK_REALM || 'camunda-platform')
                    .click();
                const mainLocator = this.page.locator('#kc-main-content-page-container');
                await (0, test_1.expect)(mainLocator).toBeVisible();
                return;
            }
            catch (error) {
                if (attempt === maxRetries) {
                    throw new Error(`switchToCamundaPlatform failed after ${maxRetries} attempts: ${error}`);
                }
                console.warn(`switchToCamundaPlatform attempt ${attempt} failed: ${error}. Retrying...`);
                await (0, sleep_1.sleep)(3000);
            }
        }
    }
    async clickUsersTab() {
        await this.page.click('[id="nav-item-users"]');
    }
    async clickAddUser() {
        const addUserButton = this.page.getByTestId('add-user');
        await (0, test_1.expect)(addUserButton).toBeVisible({ timeout: 30000 });
        await addUserButton.click({ timeout: 30000 });
    }
    async fillUser(username) {
        await this.page.getByTestId('username').fill(username);
        await this.page.getByTestId('firstName').fill(username);
        await this.page.getByTestId('lastName').fill(username);
        await this.page.getByTestId('email').fill(username + '@camunda.com');
    }
    async saveUser() {
        await this.page.getByTestId('user-creation-save').click();
    }
    async fillPassword(password, maxRetries = 3) {
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                await (0, test_1.expect)(this.credentialsTab).toBeVisible();
                await this.credentialsTab.click();
                await (0, test_1.expect)(this.noCredentialsEmptyAction).toBeVisible({
                    timeout: 60000,
                });
                await this.noCredentialsEmptyAction.click();
                await (0, test_1.expect)(this.passwordField).toBeVisible();
                await this.passwordField.fill(password);
                await (0, test_1.expect)(this.passwordConfirmationField).toBeVisible();
                await this.passwordConfirmationField.fill(password);
                return;
            }
            catch (error) {
                if (attempt === maxRetries) {
                    throw new Error(`fillPassword failed after ${maxRetries} attempts: ${error}`);
                }
                console.warn(`fillPassword attempt ${attempt} failed: ${error}. Retrying...`);
                await (0, sleep_1.sleep)(3000);
            }
        }
    }
    async savePassword() {
        await this.page
            .locator('label.pf-v5-c-switch[for="temporaryPassword"]')
            .click();
        await this.page.getByTestId('confirm').click();
        await this.page.getByTestId('confirm').click();
        // if the browser is firefox, delay for 500 ms
        if (await this.page.evaluate(() => navigator.userAgent.includes('Firefox'))) {
            await (0, sleep_1.sleep)(500);
        }
    }
}
exports.KeycloakAdminPage = KeycloakAdminPage;
