"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentityTenantPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class IdentityTenantPage {
    page;
    createTenantMainButton;
    enterTenantNameInput;
    enterTenantIdInput;
    createTenantSubButton;
    assignedUsersTab;
    assignUsersButton;
    searchUsersInput;
    assignUserFinalButton;
    removeUserButton;
    removeButton;
    openSidebarButton;
    logoutButton;
    assignedApplicationsTab;
    searchApplicationsInput;
    assignApplicationsButtonFinal;
    assignApplicationsButton;
    rows;
    confirmDeleteInput;
    dangerDeleteButton;
    cancelButton;
    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.getByRole('button', {
            name: 'Create tenant',
            exact: true,
        });
        this.assignedUsersTab = page.getByRole('tab', { name: 'Assigned users' });
        this.assignUsersButton = page.getByRole('button', { name: 'Assign users' });
        this.searchUsersInput = page.getByPlaceholder('Search users');
        this.assignUserFinalButton = page
            .getByLabel('Assign users')
            .getByRole('button', { name: 'Assign users' });
        this.removeUserButton = page.getByLabel('Remove user');
        this.removeButton = page.getByRole('button', { name: /danger\s*Remove/i });
        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.rows = this.page.getByRole('row');
        this.confirmDeleteInput = page.getByRole('textbox', {
            name: 'Type the word DELETE to confirm',
        });
        this.dangerDeleteButton = page.getByRole('button', { name: 'danger Delete' });
        this.cancelButton = page.getByRole('button', { name: 'Cancel' });
    }
    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 clickAssignedUsersTab() {
        await this.assignedUsersTab.click();
    }
    async clickAssignUsersButton() {
        await this.assignUsersButton.click();
    }
    async clickSearchUsersInput() {
        await this.searchUsersInput.click();
    }
    async fillSearchUsersInput(user) {
        await this.searchUsersInput.fill(user);
    }
    async clickAssignUserFinalButton() {
        await this.assignUserFinalButton.click();
    }
    async clickRemoveUserButton() {
        await this.removeUserButton.click();
    }
    async clickRemoveButton() {
        try {
            await this.removeButton.click({ timeout: 1000 });
        }
        catch (error) {
            try {
                await this.page
                    .getByRole('button', { name: 'dangerRemove' })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .getByRole('button', { name: 'Remove' })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .getByRole('button', { name: /Remove/i })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .locator('button.cds--btn--danger', { hasText: 'Remove' })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .getByRole('button', { name: 'danger Remove' })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .getByRole('button', { name: 'dangerRemove' })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
            try {
                await this.page
                    .getByRole('button', { name: /danger\s*Remove/i })
                    .click({ timeout: 1000 });
            }
            catch (error) {
                console.log('dangerRemove button not found');
            }
        }
    }
    async createTenant(tenantName) {
        await this.page.waitForLoadState('networkidle');
        await (0, sleep_1.sleep)(1000);
        const tenant = this.rows.filter({ hasText: tenantName }).first();
        // Check if the tenant already exists. If so, we're done.
        if (await tenant.isVisible({ timeout: 5000 })) {
            return;
        }
        // Tenant doesn't seem to exist, so let's create it.
        await this.clickCreateTenantMainButton();
        await this.clickEnterTenantNameInput();
        await this.fillEnterTenantNameInput(tenantName);
        await this.clickEnterTenantIdInput();
        await this.fillEnterTenantIdInput(tenantName);
        await this.clickCreateTenantSubButton();
        // After attempting to create, we expect either a success message,
        // or for the tenant to now be in the list (in case of a race condition
        // where another process created it).
        const toast = this.page.getByText('Tenant created');
        const errorToast = this.page.getByText('An error occurred');
        const row = this.page.getByRole('row', { name: tenantName });
        const result = await Promise.race([
            toast
                .waitFor({ state: 'visible', timeout: 10000 })
                .then(() => 'Tenant Created'),
            row
                .waitFor({ state: 'visible', timeout: 10000 })
                .then(() => 'Tenant Created'),
            errorToast
                .waitFor({ state: 'visible', timeout: 10000 })
                .then(() => 'Tenant Creation Failed'),
        ]);
        if (result === 'Tenant Created') {
            console.log(`Tenant ${tenantName} created.`);
            return;
        }
        else if (result === 'Tenant Creation Failed') {
            console.log(`Tenant ${tenantName} already exists.`);
            await this.cancelButton.click();
            await this.page.reload();
            return;
        }
        else {
            throw new Error(`Unknown error creating tenant ${tenantName}`);
        }
    }
    async deleteTenant(tenantName, tenant) {
        console.log(`Tenant ${tenantName} already exists, deleting.`);
        const deleteButton = tenant.getByRole('button', { name: 'Delete' });
        if (await deleteButton.isVisible()) {
            await deleteButton.click();
            await this.confirmDeleteInput.click();
            await this.confirmDeleteInput.fill('DELETE');
            const dangerDeleteButtons = await this.dangerDeleteButton.all();
            const lastDangerDeleteButton = dangerDeleteButtons[dangerDeleteButtons.length - 1];
            await lastDangerDeleteButton.click();
            await (0, test_1.expect)(this.page.getByText('Deleting...')).not.toBeVisible({
                timeout: 60000,
            });
            await (0, sleep_1.sleep)(1000);
        }
    }
    async assignUserToTenant(tenantName, user, userEmail) {
        await this.page.reload();
        const tenant = this.rows.filter({ hasText: tenantName }).first();
        await (0, test_1.expect)(tenant).toBeVisible();
        await tenant.click();
        await this.clickAssignedUsersTab();
        await this.clickAssignUsersButton();
        await this.clickSearchUsersInput();
        await this.fillSearchUsersInput(user);
        await (0, test_1.expect)(this.page.getByText(userEmail)).toBeVisible({ timeout: 30000 });
        await this.page.getByText(userEmail).click();
        await this.clickAssignUserFinalButton();
        await (0, test_1.expect)(this.page.getByText('Users assigned')).toBeVisible({
            timeout: 60000,
        });
    }
    async removeUserFromTenant(tenantName) {
        const tenant = this.rows.filter({ hasText: tenantName }).first();
        await (0, test_1.expect)(tenant).toBeVisible();
        await tenant.click();
        await this.clickAssignedUsersTab();
        await this.clickRemoveUserButton();
        await this.clickRemoveButton();
        /*await expect(this.page.getByText('users.Entity 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();
    }
    async clickSearchApplicationsInput() {
        await this.searchApplicationsInput.click();
    }
    async fillSearchApplicationsInput(clientName) {
        await this.searchApplicationsInput.fill(clientName);
    }
    async clickAssignApplicationsFinalButton() {
        await this.assignApplicationsButtonFinal.click({ timeout: 60000 });
    }
    async clickAssignApplicationsButton() {
        await this.assignApplicationsButton.click({ timeout: 60000 });
    }
}
exports.IdentityTenantPage = IdentityTenantPage;
