"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;
    searchBoxResult;
    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.searchBoxResult = page.getByRole('listitem');
        this.assignUserButtonModal = page
            .getByLabel('Assign user')
            .getByRole('button', { name: 'Assign user' });
    }
    async createGroup(groupId, groupName, description) {
        await this.createGroupButton.click();
        await this.createGroupIdField.fill(groupId);
        await this.createNameField.fill(groupName);
        if (description) {
            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 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) {
        for (let i = 0; i < 10; i++) {
            try {
                await this.selectGroupRow(groupName).click({ timeout: 15000 });
                return;
            }
            catch (err) {
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Failed to click group "${groupName}" after 10 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;
