"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIdentityRolesPage = void 0;
const test_1 = require("@playwright/test");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
const expectLocatorWithPagination_1 = require("../../utils/assertionHelpers/expectLocatorWithPagination");
class OCIdentityRolesPage {
    page;
    selectResourceTypeTab;
    assignMappingRuleButton;
    mappingSearchBox;
    assignMappingModal;
    assignMappingModalSearchResult;
    assignMappingModalAssignRuleButton;
    rolesList;
    roleCell;
    rowValue;
    createRoleButton;
    createRoleModal;
    createNameField;
    createIdField;
    createRoleModalCreateButton;
    deleteRoleButton;
    deleteRoleModal;
    closeDeleteRoleModal;
    deleteRoleModalCancelButton;
    deleteRoleModalDeleteButton;
    assignUserButton;
    assignUserModal;
    assignUserModalInputField;
    assignUserModalAssignButton;
    constructor(page) {
        this.page = page;
        this.rolesList = page.getByRole('table');
        this.selectResourceTypeTab = (resourceType) => page.getByRole('tab', { name: resourceType });
        this.assignMappingRuleButton = page.getByRole('button', {
            name: /^(Assign mapping rule|assignMapping)$/,
        });
        this.mappingSearchBox = page.getByRole('searchbox', {
            name: 'Search by mapping rule ID',
        });
        this.assignMappingModal = page.getByRole('dialog', {
            name: 'Assign mapping rule',
        });
        this.createRoleButton = page.getByRole('button', {
            name: /create role/i,
        });
        this.assignMappingModalSearchResult =
            this.assignMappingModal.getByRole('listbox');
        this.assignMappingModalAssignRuleButton = this.assignMappingModal.getByRole('button', {
            name: 'Assign mapping rule',
        });
        this.roleCell = (roleID) => this.rolesList.getByRole('cell', { name: roleID, exact: true });
        this.rowValue = (name) => page.getByRole('row').filter({ hasText: name });
        this.createRoleModal = page.getByRole('dialog', {
            name: 'Create role',
        });
        this.createIdField = this.createRoleModal.getByRole('textbox', {
            name: 'Role ID',
            exact: true,
        });
        this.createNameField = this.createRoleModal.getByRole('textbox', {
            name: 'Role name',
            exact: true,
        });
        this.createRoleModalCreateButton = this.createRoleModal.getByRole('button', { name: 'Create role' });
        this.deleteRoleButton = (rowName) => this.rolesList.getByRole('row', { name: rowName }).getByLabel('Delete');
        this.deleteRoleModal = page.getByRole('dialog', { name: /delete role/i });
        this.closeDeleteRoleModal = this.deleteRoleModal.getByRole('button', {
            name: 'Close',
        });
        this.deleteRoleModalCancelButton = this.deleteRoleModal.getByRole('button', {
            name: 'Cancel',
        });
        this.deleteRoleModalDeleteButton = this.deleteRoleModal.getByRole('button', {
            name: /delete role/i,
        });
        this.assignUserButton = page.getByRole('button', {
            name: /Assign user/i,
        });
        this.assignUserModal = page.getByRole('dialog', {
            name: 'Assign user',
        });
        this.assignUserModalInputField =
            this.assignUserModal.getByTitle('Username');
        this.assignUserModalAssignButton = this.assignUserModal.getByRole('button', {
            name: /assign user/i,
        });
    }
    async clickMappingRulesTab() {
        await (0, test_1.expect)(this.selectResourceTypeTab('Mapping Rules')).toBeVisible({
            timeout: 60000,
        });
        await this.selectResourceTypeTab('Mapping Rules').click();
    }
    async clickRole(roleID) {
        await (0, test_1.expect)(this.roleCell(roleID).first()).toBeVisible({ timeout: 60000 });
        await this.roleCell(roleID).first().click();
    }
    async assignMappingRuleToRole(roleId, mappingId) {
        await this.clickRole(roleId);
        await this.clickMappingRulesTab();
        await (0, test_1.expect)(this.assignMappingRuleButton).toBeVisible({
            timeout: 60000,
        });
        await this.assignMappingRuleButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.assignMappingModal).toBeVisible({ timeout: 60000 });
        await this.mappingSearchBox.fill(mappingId);
        await this.assignMappingModalSearchResult
            .getByText(mappingId)
            .click({ timeout: 30000 });
        await this.assignMappingModalAssignRuleButton.click({ timeout: 30000 });
    }
    async doAssignUserToRole(roleId, email) {
        await this.clickRole(roleId);
        await (0, test_1.expect)(this.assignUserButton).toBeVisible({
            timeout: 60000,
        });
        await this.assignUserButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.assignUserModal).toBeVisible({ timeout: 30000 });
        await this.assignUserModalInputField.fill(email);
        await this.assignUserModalAssignButton.click({ timeout: 30000 });
        await (0, test_1.expect)(this.assignUserModal).not.toBeVisible({ timeout: 30000 });
    }
    async createRole(role) {
        await (0, test_1.expect)(this.createRoleButton).toBeVisible({ timeout: 30000 });
        await this.createRoleButton.click({ timeout: 30000 });
        await (0, test_1.expect)(this.createRoleModal).toBeVisible({ timeout: 30000 });
        await this.createIdField.fill(role.id);
        await this.createNameField.fill(role.name);
        await this.createRoleModalCreateButton.click();
        await (0, test_1.expect)(this.createRoleModal).not.toBeVisible({ timeout: 30000 });
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, this.rolesList
            .getByRole('cell', {
            name: role.name,
        })
            .first());
    }
    async assignMappingToRole(role, mappingRuleId) {
        try {
            await (0, test_1.expect)(this.page.getByText(role).first()).toBeVisible({
                timeout: 10000,
            });
        }
        catch (error) {
            await this.createRole({ id: role, name: role });
        }
        await this.assignMappingRuleToRole(role, mappingRuleId);
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.rowValue(mappingRuleId).first());
        await (0, test_1.expect)(this.rowValue(mappingRuleId)).toContainText(`${mappingRuleId}`);
    }
    async assignUserToRole(role, email) {
        try {
            await (0, test_1.expect)(this.page.getByText(role).first()).toBeVisible({
                timeout: 10000,
            });
        }
        catch (error) {
            await this.createRole({ id: role, name: role });
        }
        await this.doAssignUserToRole(role, email);
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.rowValue(email).first(), {
            preAction: async () => {
                await this.page.reload();
                await (0, test_1.expect)(this.rolesList.first()).toBeVisible({ timeout: 20000 });
            },
        });
        await (0, test_1.expect)(this.rowValue(email)).toContainText(`${email}`);
    }
    async deleteRole(name) {
        const locator = this.deleteRoleButton(name);
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, locator);
        await locator.click();
        await (0, test_1.expect)(this.deleteRoleModal).toBeVisible();
        await this.deleteRoleModalDeleteButton.click();
        await (0, test_1.expect)(this.deleteRoleModal).not.toBeVisible();
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, locator, {
            shouldBeVisible: false,
        });
    }
}
exports.OCIdentityRolesPage = OCIdentityRolesPage;
