"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIdentityGroupsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OCIdentityGroupsPage {
    page;
    groupsList;
    createGroupButton;
    editGroupButton;
    deleteGroupButton;
    createGroupModal;
    closeCreateGroupModal;
    createGroupIdField;
    createNameField;
    createDescriptionField;
    createGroupModalCancelButton;
    createGroupModalCreateButton;
    editGroupModal;
    closeEditGroupModal;
    editNameField;
    editDescriptionField;
    editGroupModalCancelButton;
    editGroupModalUpdateButton;
    deleteGroupModal;
    closeDeleteGroupModal;
    deleteGroupModalCancelButton;
    deleteGroupModalDeleteButton;
    emptyState;
    assignUserButton;
    searchBox;
    assignUserButtonModal;
    selectGroupRow;
    groupCell;
    constructor(page) {
        this.page = page;
        this.groupsList = page.getByRole('table');
        this.selectGroupRow = (name) => this.groupsList.getByRole('row', { name: name });
        this.groupCell = (name) => this.groupsList.getByRole('cell', { name, exact: true });
        this.createGroupButton = page.getByRole('button', {
            name: /Create( a)? group/,
        });
        this.editGroupButton = (rowName) => this.groupsList.getByRole('row', { name: rowName }).getByLabel('Edit');
        this.deleteGroupButton = (rowName) => this.groupsList.getByRole('row', { name: rowName }).getByLabel('Delete');
        this.createGroupModal = page.getByRole('dialog', {
            name: 'Create group',
        });
        this.closeCreateGroupModal = this.createGroupModal.getByRole('button', {
            name: 'Close',
        });
        this.createGroupIdField = this.createGroupModal.getByRole('textbox', {
            name: 'Group ID',
        });
        this.createNameField = this.createGroupModal.getByRole('textbox', {
            name: 'Group name',
        });
        this.createDescriptionField = this.createGroupModal.getByRole('textbox', {
            name: 'Description',
        });
        this.createGroupModalCancelButton = this.createGroupModal.getByRole('button', {
            name: 'Cancel',
        });
        this.createGroupModalCreateButton = this.createGroupModal.getByRole('button', {
            name: 'Create group',
        });
        this.editGroupModal = page.getByRole('dialog', {
            name: 'Edit group',
        });
        this.closeEditGroupModal = this.editGroupModal.getByRole('button', {
            name: 'Close',
        });
        this.editNameField = this.editGroupModal.getByRole('textbox', {
            name: 'Group name',
        });
        this.editDescriptionField = this.editGroupModal.getByRole('textbox', {
            name: 'Description',
        });
        this.editGroupModalCancelButton = this.editGroupModal.getByRole('button', {
            name: 'Cancel',
        });
        this.editGroupModalUpdateButton = this.editGroupModal.getByRole('button', {
            name: 'Edit group',
        });
        this.deleteGroupModal = page.getByRole('dialog', {
            name: 'Delete group',
        });
        this.closeDeleteGroupModal = this.deleteGroupModal.getByRole('button', {
            name: 'Close',
        });
        this.deleteGroupModalCancelButton = this.deleteGroupModal.getByRole('button', {
            name: 'Cancel',
        });
        this.deleteGroupModalDeleteButton = this.deleteGroupModal.getByRole('button', {
            name: 'Delete group',
        });
        this.emptyState = page.getByText('No groups created yet');
        this.assignUserButton = page.getByRole('button', { name: 'Assign user' });
        this.searchBox = page.getByTitle('Username');
        this.assignUserButtonModal = page
            .getByLabel('Assign user')
            .getByRole('button', { name: 'Assign user' });
    }
    async createGroup(groupId, groupName, description) {
        await (0, test_1.expect)(this.createGroupButton).toBeVisible({ timeout: 10000 });
        await this.createGroupButton.click();
        await (0, test_1.expect)(this.createGroupIdField).toBeVisible({ timeout: 10000 });
        await this.createGroupIdField.click();
        await this.createGroupIdField.fill(groupId);
        await (0, test_1.expect)(this.createNameField).toBeVisible({ timeout: 10000 });
        await this.createNameField.click();
        await this.createNameField.fill(groupName);
        if (description) {
            await (0, test_1.expect)(this.createDescriptionField).toBeVisible({ timeout: 10000 });
            await this.createDescriptionField.fill(description);
        }
        await this.createGroupModalCreateButton.click();
        await (0, test_1.expect)(this.createGroupModal).toBeHidden();
    }
    async editGroup(currentName, newName, newDescription) {
        await this.editGroupButton(currentName).click();
        await (0, test_1.expect)(this.editGroupModal).toBeVisible();
        await this.editNameField.fill(newName);
        if (newDescription) {
            await this.editDescriptionField.fill(newDescription);
        }
        await this.editGroupModalUpdateButton.click();
        await (0, test_1.expect)(this.editGroupModal).toBeHidden();
    }
    async deleteGroup(groupName) {
        await this.deleteGroupButton(groupName).click();
        await (0, test_1.expect)(this.deleteGroupModal).toBeVisible();
        await (0, test_1.expect)(this.deleteGroupModalDeleteButton).toBeVisible();
        await this.deleteGroupModalDeleteButton.click();
        await (0, test_1.expect)(this.deleteGroupModal).toBeHidden();
    }
    async assertGroupExists(groupName) {
        await (0, test_1.expect)(this.selectGroupRow(groupName)).toBeVisible();
    }
    async clickGroupId(groupName) {
        const maxRetries = 10;
        const retryDelay = 5000;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                const groupRow = await this.selectGroupRow(groupName);
                await groupRow.click();
                return;
            }
            catch (error) {
                if (attempt < maxRetries) {
                    console.warn(`Attempt ${attempt} failed. Reloading and retrying in 5 seconds...`);
                    await this.page.reload();
                    await this.page.waitForTimeout(retryDelay);
                }
                else {
                    throw new Error(`Failed to click group '${groupName}' after ${maxRetries} attempts.`);
                }
            }
        }
    }
    async assignUserToGroup(userName) {
        await this.assignUserButton.click();
        await this.searchBox.fill(userName);
        await this.assignUserButtonModal.click();
        await (0, sleep_1.sleep)(8000);
    }
}
exports.OCIdentityGroupsPage = OCIdentityGroupsPage;
