"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;
    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.assignApplicationsButton = page.getByRole('button', {
            name: 'Assign applications',
        });
        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 Remove' });
        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.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' });
    }
    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({ timeout: 60000 });
    }
    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() {
        await this.removeButton.click();
    }
    async createTenant(tenantName) {
        await (0, sleep_1.sleep)(5000);
        const tenant = this.rows.filter({ hasText: tenantName }).first();
        if (await tenant.isVisible({ timeout: 20000 })) {
            await this.deleteTenant(tenantName, tenant);
        }
        await this.clickCreateTenantMainButton();
        await this.clickEnterTenantNameInput();
        await this.fillEnterTenantNameInput(tenantName);
        await this.clickEnterTenantIdInput();
        await this.fillEnterTenantIdInput(tenantName);
        await this.clickCreateTenantSubButton();
        await (0, test_1.expect)(this.page.getByText('Tenant created')).toBeVisible({
            timeout: 60000,
        });
        await (0, sleep_1.sleep)(30000);
    }
    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({ force: true, timeout: 60000 });
            await this.confirmDeleteInput.click({ force: true, timeout: 60000 });
            await this.confirmDeleteInput.fill('DELETE', { timeout: 60000 });
            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.getByText(tenantName).first().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', { exact: true })).toBeVisible({
            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').first()).toBeVisible({
                timeout: 60000,
            });
        }
    }
    async removeUserFromTenant(tenantName) {
        await this.page.getByText(tenantName).first().click();
        await this.clickAssignedUsersTab();
        await this.clickRemoveUserButton();
        await this.clickRemoveButton();
        await (0, test_1.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 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 });
    }
}
exports.IdentityTenantPage = IdentityTenantPage;
