"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIdentityRolesPage = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../SM-8.10/UtilitiesPage");
const expectLocatorWithPagination_1 = require("../../utils/assertionHelpers/expectLocatorWithPagination");
const submitModalWithRetry_1 = require("../../utils/assertionHelpers/submitModalWithRetry");
const sleep_1 = require("../../utils/sleep");
class OCIdentityRolesPage {
    page;
    selectResourceTypeTab;
    assignMappingRuleButton;
    mappingSearchBox;
    assignMappingModal;
    assignMappingModalSearchResult;
    assignMappingModalAssignRuleButton;
    rolesList;
    roleCell;
    mappingRuleRow;
    createRoleButton;
    createRoleModal;
    createNameField;
    createIdField;
    createRoleModalCreateButton;
    deleteRoleButton;
    deleteRoleModal;
    closeDeleteRoleModal;
    deleteRoleModalCancelButton;
    deleteRoleModalDeleteButton;
    adminRole;
    assignUserButton;
    usernameTextBox;
    assignUserSubButton;
    clientsTab;
    assignClientsButton;
    clientIdTextBox;
    assignClientIdSubButton;
    notFoundText;
    constructor(page) {
        this.page = page;
        this.rolesList = page.getByRole('table');
        this.notFoundText = page.getByText('404 not found');
        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.adminRole = page.getByRole('cell', { name: 'admin', exact: true });
        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.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' });
    }
    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) {
        const maxRetries = 3;
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            await this.clickRole(roleId);
            // After clicking the role, the detail page may show a 404 if the role
            // was just created and the backend hasn't fully provisioned it yet.
            const gotNotFound = await this.notFoundText
                .isVisible({ timeout: 3000 })
                .catch(() => false);
            if (gotNotFound) {
                console.warn(`Attempt ${attempt + 1}: Role detail page returned 404 for "${roleId}", retrying...`);
                await this.page.goBack();
                await (0, sleep_1.sleep)(3000);
                continue;
            }
            try {
                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();
                return;
            }
            catch (error) {
                if (attempt < maxRetries - 1) {
                    console.warn(`Attempt ${attempt + 1} failed to assign mapping rule to role "${roleId}". Retrying...`);
                    await this.page.goBack();
                    await (0, sleep_1.sleep)(2000);
                }
                else {
                    throw error;
                }
            }
        }
    }
    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 (0, submitModalWithRetry_1.submitModalWithRetry)(this.createRoleModalCreateButton, this.createRoleModal);
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, this.rolesList
            .getByRole('cell', {
            name: role.name,
        })
            .first(), { maxRetries: 10 });
    }
    async deleteRole(name, options) {
        const { throwError = true } = options || {};
        try {
            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 clickAdminRole() {
        await this.adminRole.click();
    }
    async assignUserToRole(userId) {
        await this.clickAssignUserButton();
        await this.clickUsernameTextbox();
        await this.fillUsernameTextbox(userId);
        await this.clickAssignUserSubButton();
    }
    async clickAssignUserButton() {
        await this.assignUserButton.click();
    }
    async clickUsernameTextbox() {
        await this.usernameTextBox.click();
    }
    async fillUsernameTextbox(userId) {
        await this.usernameTextBox.fill(userId);
    }
    async clickAssignUserSubButton() {
        await this.assignUserSubButton.click();
    }
    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;
