"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCTenantPage = void 0;
const test_1 = require("@playwright/test");
const expectTextWithPagination_1 = require("../../utils/assertionHelpers/expectTextWithPagination");
class OCTenantPage {
    page;
    createTenantMainButton;
    enterTenantNameInput;
    enterTenantIdInput;
    createTenantSubButton;
    usersTab;
    assignUsersButton;
    usernameInput;
    assignUserFinalButton;
    removeUserButton;
    removeButton;
    openSidebarButton;
    logoutButton;
    assignedApplicationsTab;
    searchApplicationsInput;
    assignApplicationsButtonFinal;
    assignApplicationsButton;
    tenantDescriptionInput;
    modalCloseButton;
    rolesTab;
    assignRoleButton;
    roleIdSearchBox;
    assignRoleSubButton;
    cancelButton;
    gotItButton;
    filterTableSearchbox;
    row;
    constructor(page) {
        this.page = page;
        this.createTenantMainButton = page.getByRole('button', {
            name: 'Create Tenant',
        });
        this.enterTenantNameInput = page.getByPlaceholder('Enter tenant name');
        this.enterTenantIdInput = page.getByPlaceholder('Enter tenant ID');
        this.createTenantSubButton = page
            .getByLabel('Create new tenant')
            .getByRole('button', { name: 'Create tenant' });
        this.usersTab = page.getByRole('tab', { name: 'Users' });
        this.assignUsersButton = page.getByRole('button', { name: 'Assign user' });
        this.usernameInput = page.getByTitle('Username');
        this.assignUserFinalButton = page
            .getByLabel('Assign user')
            .getByRole('button', { name: 'Assign user' });
        this.removeUserButton = page
            .getByRole('dialog', { name: 'Remove user' })
            .getByRole('button', { name: 'Remove user' });
        this.removeButton = page.getByRole('button', { name: 'Remove', exact: true });
        this.openSidebarButton = page.getByLabel('Open User Sidebar');
        this.logoutButton = page.getByRole('button', { name: 'Log out' });
        this.assignedApplicationsTab = page.getByRole('tab', {
            name: 'Assigned applications',
        });
        this.searchApplicationsInput = page.getByPlaceholder('Search applications');
        this.assignApplicationsButtonFinal = page
            .getByLabel('Assign applications')
            .getByRole('button', { name: 'Assign applications' });
        this.assignApplicationsButton = page.getByRole('button', {
            name: 'Assign applications',
        });
        this.tenantDescriptionInput = page.getByTitle('Description');
        this.modalCloseButton = page
            .getByRole('dialog')
            .getByRole('button', { name: 'Close' });
        this.rolesTab = page.getByRole('tab', { name: 'Roles' });
        this.assignRoleButton = page.getByRole('button', { name: 'Assign role' });
        this.roleIdSearchBox = page.getByRole('searchbox', {
            name: 'Search by Role ID',
        });
        this.assignRoleSubButton = page
            .getByLabel('Assign role')
            .getByRole('button', { name: 'Assign role' });
        this.cancelButton = page
            .getByRole('dialog')
            .getByRole('button', { name: 'Cancel' });
        this.gotItButton = page.getByRole('button', { name: 'Got it' });
        this.filterTableSearchbox = page.getByRole('searchbox', {
            name: 'Filter table',
        });
        this.row = (name) => this.page.getByRole('row').filter({ hasText: name }).first();
    }
    async clickCreateTenantMainButton() {
        await this.createTenantMainButton.click({ timeout: 60000 });
    }
    async clickCreateTenantSubButton() {
        await this.createTenantSubButton.click({ timeout: 60000 });
    }
    async clickEnterTenantNameInput() {
        await this.enterTenantNameInput.click();
    }
    async fillEnterTenantNameInput(tenantName) {
        await this.enterTenantNameInput.fill(tenantName);
    }
    async clickEnterTenantIdInput() {
        await this.enterTenantIdInput.click();
    }
    async fillEnterTenantIdInput(tenantId) {
        await this.enterTenantIdInput.fill(tenantId);
    }
    async clickUsersTab() {
        await this.usersTab.click({ timeout: 60000 });
    }
    async clickAssignUsersButton() {
        await this.assignUsersButton.click();
    }
    async clickUsernameInput() {
        await this.usernameInput.click();
    }
    async fillUsernameInput(user) {
        await this.usernameInput.fill(user);
    }
    async clickAssignUserFinalButton() {
        try {
            await this.assignUserFinalButton.click();
            await (0, test_1.expect)(this.page.getByRole('dialog')).not.toBeVisible();
        }
        catch (e) {
            if (await this.page.getByText('ALREADY_EXISTS').first().isVisible()) {
                console.log('User is already assigned to tenant');
                await this.clickCancelButton();
            }
            else {
                throw new Error('Assigning user to tenant failed' + e);
            }
        }
    }
    async clickRemoveUserButton() {
        await this.removeUserButton.click();
    }
    async clickRemoveButton() {
        await this.removeButton.click();
    }
    async clickTenantDescriptionInput() {
        await this.tenantDescriptionInput.click();
    }
    async fillTenantDescriptionInput(description) {
        await this.tenantDescriptionInput.fill(description);
    }
    async createTenant(tenantName) {
        await this.clickCreateTenantMainButton();
        await this.clickEnterTenantNameInput();
        await this.fillEnterTenantNameInput(tenantName);
        await this.clickEnterTenantIdInput();
        await this.fillEnterTenantIdInput(tenantName);
        await this.clickTenantDescriptionInput();
        await this.fillTenantDescriptionInput('This is a test');
        await this.clickCreateTenantSubButton();
        await this.assertTenantCreated();
    }
    async assertTenantCreated() {
        try {
            await (0, test_1.expect)(this.page.getByText('successfully created')).toBeVisible({
                timeout: 8000,
            });
            await this.clickGotItButton();
        }
        catch (error) {
            if (await this.page.getByText('ALREADY_EXISTS').first().isVisible()) {
                await this.clickCancelButton();
            }
            else {
                throw new Error('Creating tenant failed' + error);
            }
        }
    }
    async clickCancelButton() {
        await this.cancelButton.click({ timeout: 30000 });
    }
    async clickGotItButton() {
        await this.gotItButton.click({ timeout: 30000 });
    }
    async filterTable(tenantName) {
        await this.filterTableSearchbox.click({ timeout: 60000 });
        await this.filterTableSearchbox.fill(tenantName);
        await this.filterTableSearchbox.press('Enter');
    }
    async clickTenant(tenantName) {
        const maxRetries = 10;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                await this.page.reload();
                await this.page.waitForLoadState('networkidle');
                await this.filterTable(tenantName);
                const tenant = this.page.getByText(tenantName).first();
                await (0, test_1.expect)(tenant).toBeVisible({ timeout: 30000 });
                await tenant.click({ timeout: 30000 });
                return;
            }
            catch (error) {
                console.warn(`Attempt ${attempt} to click tenant '${tenantName}' failed. Retrying...`, error);
                if (attempt === maxRetries) {
                    throw new Error(`Failed to click tenant '${tenantName}' after ${maxRetries} attempts.`);
                }
            }
        }
    }
    async assignUserToTenant(user) {
        await this.clickUsersTab();
        await this.clickAssignUsersButton();
        await this.clickUsernameInput();
        await this.fillUsernameInput(user);
        await this.clickAssignUserFinalButton();
    }
    async removeUserFromTenant(tenantName) {
        await (0, expectTextWithPagination_1.expectTextWithPagination)(this.page, tenantName);
        await this.page.getByText(tenantName).first().click();
        await this.clickUsersTab();
        await this.clickRemoveButton();
        await this.clickRemoveUserButton();
        await (0, test_1.expect)(this.page.getByText('Tenant member has been removed.')).toBeVisible({
            timeout: 60000,
        });
    }
    async clickOpenSidebarButton() {
        await this.openSidebarButton.click({ timeout: 60000 });
    }
    async clickLogoutButton() {
        await this.logoutButton.click({ timeout: 60000 });
    }
    async assignClientToTenant(clientName) {
        await this.clickAssignedApplicationsTab();
        if (await this.page.getByText(clientName, { exact: true }).isVisible()) {
            return;
        }
        else {
            await this.clickAssignApplicationsButton();
            await this.clickSearchApplicationsInput();
            await this.fillSearchApplicationsInput(clientName);
            await this.page.getByText(clientName, { exact: true }).click();
            await this.clickAssignApplicationsFinalButton();
            await (0, test_1.expect)(this.page.getByText('Applications assigned')).toBeVisible({
                timeout: 60000,
            });
        }
    }
    async clickAssignedApplicationsTab() {
        await this.assignedApplicationsTab.click({ timeout: 60000 });
    }
    async clickSearchApplicationsInput() {
        await this.searchApplicationsInput.click({ timeout: 60000 });
    }
    async fillSearchApplicationsInput(clientName) {
        await this.searchApplicationsInput.fill(clientName);
    }
    async clickAssignApplicationsFinalButton() {
        await this.assignApplicationsButtonFinal.click({ timeout: 60000 });
    }
    async clickAssignApplicationsButton() {
        await this.assignApplicationsButton.click({ timeout: 60000 });
    }
    async clickRolesTab() {
        await this.rolesTab.click();
    }
    async clickAssignRoleButton() {
        await this.assignRoleButton.click();
    }
    async clickRoleIdSearchBox() {
        await this.roleIdSearchBox.click();
    }
    async fillRoleIdSearchBox(roleId, roleIdentifier) {
        await this.roleIdSearchBox.fill(roleId);
        const result = this.page.getByText(roleIdentifier).first();
        await (0, test_1.expect)(result).toBeVisible({ timeout: 15000 });
        await result.click();
    }
    async clickAssignRoleSubButton() {
        await this.assignRoleSubButton.click();
    }
    async assignRoleToTenant(roleId, roleIdentifier) {
        await this.clickRolesTab();
        await this.page.waitForLoadState('networkidle');
        if (await this.row(roleId).isVisible()) {
            console.log(`Role ${roleId} is already assigned to tenant`);
            return;
        }
        await this.clickAssignRoleButton();
        await this.clickRoleIdSearchBox();
        await this.fillRoleIdSearchBox(roleId, roleIdentifier);
        await this.clickAssignRoleSubButton();
        await (0, test_1.expect)(this.page.getByLabel('Assign role')).not.toBeVisible({
            timeout: 30000,
        });
        // Use retry-with-reload pattern for role visibility (MT operations need time for data replication)
        await (0, test_1.expect)(async () => {
            if (!(await this.row(roleId).isVisible())) {
                await this.page.reload();
                await this.page.waitForLoadState('networkidle');
            }
            await (0, test_1.expect)(this.row(roleId)).toBeVisible({ timeout: 10000 });
        }).toPass({ timeout: 60000, intervals: [5000] });
    }
}
exports.OCTenantPage = OCTenantPage;
