"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIdentityRolesPage = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../SM-8.8/UtilitiesPage");
const expectLocatorWithPagination_1 = require("../../utils/assertionHelpers/expectLocatorWithPagination");
const clickLocatorWithRetry_1 = require("../../utils/assertionHelpers/clickLocatorWithRetry");
class OCIdentityRolesPage {
    page;
    selectResourceTypeTab;
    assignMappingRuleButton;
    mappingSearchBox;
    assignMappingModal;
    assignMappingModalSearchResult;
    assignMappingModalAssignRuleButton;
    rolesList;
    roleCell;
    mappingRuleRow;
    createRoleButton;
    createRoleModal;
    createNameField;
    createIdField;
    createRoleModalCreateButton;
    deleteRoleButton;
    deleteRoleModal;
    closeDeleteRoleModal;
    deleteRoleModalCancelButton;
    deleteRoleModalDeleteButton;
    assignUserButton;
    usernameTextBox;
    assignUserSubButton;
    adminRole;
    clientsTab;
    assignClientsButton;
    clientIdTextBox;
    assignClientIdSubButton;
    demoRole;
    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.mappingRuleRow = (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' });
        this.usernameTextBox = page.getByRole('textbox', { name: 'Username' });
        this.assignUserSubButton = page
            .getByLabel('Assign user')
            .getByRole('button', { name: 'Assign user' });
        this.adminRole = page.getByRole('cell', { name: 'admin', exact: true });
        this.clientsTab = page.getByRole('tab', { name: 'Clients' });
        this.assignClientsButton = page.getByRole('button', {
            name: 'Assign client',
        });
        this.clientIdTextBox = page.getByPlaceholder('Enter client ID');
        this.assignClientIdSubButton = page
            .getByLabel('Assign client')
            .getByRole('button', { name: 'Assign client' });
        this.demoRole = page.getByRole('cell', { name: 'demo' });
    }
    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();
        await this.assignMappingModalAssignRuleButton.click();
    }
    async createRole(role) {
        await this.createRoleButton.click();
        await (0, test_1.expect)(this.createRoleModal).toBeVisible();
        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();
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, this.rolesList
            .getByRole('cell', {
            name: role.name,
        })
            .first());
    }
    async deleteRole(name, options) {
        const { throwError = true } = options || {};
        try {
            // The role may be on a later pagination page of the Carbon DataTable
            // (off-page rows are not rendered in the DOM), so paginate to it before
            // attempting to click the Delete button on its row.
            await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, this.rolesList.getByRole('cell', { name }).first());
            await this.deleteRoleButton(name).click();
            await (0, test_1.expect)(this.deleteRoleModal).toBeVisible();
            await this.deleteRoleModalDeleteButton.click();
            await (0, test_1.expect)(this.deleteRoleModal).not.toBeVisible();
            await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(this.page, this.rolesList
                .getByRole('cell', {
                name,
            })
                .first(), name, 5000, true);
        }
        catch (error) {
            if (throwError) {
                throw error;
            }
            else {
                console.warn(`Role "${name}" could not be deleted or did not exist.`);
            }
        }
    }
    async assignUserToRole(userId) {
        await this.clickAssignUserButton();
        await this.clickUsernameTextbox();
        await this.fillUsernameTextbox(userId);
        await this.clickAssignUserSubButton();
    }
    async clickAssignUserButton() {
        await (0, test_1.expect)(this.assignUserButton).toBeVisible({ timeout: 60000 });
        await this.assignUserButton.click({ timeout: 60000 });
    }
    async clickUsernameTextbox() {
        await this.usernameTextBox.click();
    }
    async fillUsernameTextbox(userId) {
        await this.usernameTextBox.fill(userId);
    }
    async clickAssignUserSubButton() {
        await this.assignUserSubButton.click();
    }
    async clickAdminRole() {
        // The Roles list is occasionally empty on first render after the Roles
        // tab loads (opensearch upgrade scenarios). Reload between attempts so
        // the table refetches instead of waiting on a stale empty state.
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, this.adminRole, {
            visibilityTimeout: 30000,
            totalTimeout: 120000,
            maxRetries: 4,
            postAction: async () => {
                await this.page.reload({ waitUntil: 'domcontentloaded' });
            },
        });
    }
    async clickClientsTab() {
        await this.clientsTab.click();
    }
    async clickAssignClientButton() {
        await this.assignClientsButton.click();
    }
    async clickClientIdTextbox() {
        await this.clientIdTextBox.click();
    }
    async fillClientIdTextbox(clientId) {
        await this.clientIdTextBox.fill(clientId);
    }
    async clickAssignClientIdSubButton() {
        await this.assignClientIdSubButton.click();
    }
    async addClientToRole(clientId) {
        await this.clickClientsTab();
        await this.clickAssignClientButton();
        await this.clickClientIdTextbox();
        await this.fillClientIdTextbox(clientId);
        await this.clickAssignClientIdSubButton();
    }
}
exports.OCIdentityRolesPage = OCIdentityRolesPage;
