"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentityPage = void 0;
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
class IdentityPage {
    page;
    identityBanner;
    usersLink;
    assignedRolesTab;
    demoUser;
    confirmDeleteButton;
    assignRolesButton;
    addButton;
    operateCheckbox;
    tasklistCheckbox;
    optimizeCheckbox;
    modelerCheckbox;
    consoleCheckbox;
    groupsTab;
    createFirstGroupButton;
    createGroupButton;
    createGroupSubButton;
    groupNameInput;
    assignMembersButton;
    assignMembersSubButton;
    assignMembersSearchbox;
    assignMembersSearchResult;
    noGoupsHeading;
    deleteButton;
    deleteSubButton;
    createGroupDialoge;
    authorizationsTab;
    tenantTab;
    licenseKeyTagNonProduction;
    licenseKeyTagNonCommercial;
    licenseKeyTagProduction;
    licenseKeyTagCommercial;
    zeebeCheckbox;
    modelerAdminCheckbox;
    identityCheckbox;
    constructor(page) {
        this.page = page;
        this.identityBanner = page.getByRole('link', {
            name: 'Camunda logo Identity',
        });
        this.usersLink = page.locator('a').filter({ hasText: 'Users' });
        this.assignedRolesTab = page.getByRole('tab', { name: 'Assigned roles' });
        this.demoUser = page.getByRole('cell', { name: 'demo' }).first();
        this.confirmDeleteButton = page.getByRole('button', {
            name: 'danger Delete',
        });
        this.assignRolesButton = page.getByRole('button', { name: 'Assign roles' });
        this.addButton = page.getByRole('button', { name: 'Add' });
        this.operateCheckbox = page
            .locator('label')
            .filter({ hasText: 'Operate (Grants full access to Operate)' });
        this.tasklistCheckbox = page
            .locator('label')
            .filter({ hasText: 'Tasklist (Grants full access to Tasklist)' });
        this.optimizeCheckbox = page
            .locator('label')
            .filter({ hasText: 'Optimize (Grants full access to Optimize)' });
        this.modelerCheckbox = page
            .locator('label')
            .filter({ hasText: 'Web Modeler (Grants full access to Web Modeler)' });
        this.consoleCheckbox = page
            .locator('label')
            .filter({ hasText: 'Console (Grants full access to Console)' });
        this.groupsTab = page.getByText('groups').first();
        this.createGroupButton = page.getByRole('button', {
            name: 'create group',
        });
        this.createFirstGroupButton = page.getByRole('button', {
            name: 'create a group',
        });
        this.createGroupSubButton = page
            .getByRole('button', {
            name: 'create group',
        })
            .last();
        this.groupNameInput = page.getByRole('textbox', { name: 'name' });
        this.assignMembersButton = page.getByRole('button', {
            name: 'assign members',
        });
        this.assignMembersSubButton = page
            .getByRole('button', {
            name: 'assign',
        })
            .last();
        this.assignMembersSearchbox = page.getByRole('searchbox', {
            name: 'search by full name or email address',
        });
        this.assignMembersSearchResult = page.locator('.cds--list-box__menu-item');
        this.noGoupsHeading = page.getByRole('heading', {
            name: 'you don’t have any groups yet',
        });
        this.deleteSubButton = page.getByRole('button', { name: 'danger delete' });
        this.deleteButton = page.locator('button', { hasText: 'danger' });
        this.createGroupDialoge = page.getByRole('dialog', {
            name: 'create group',
        });
        this.authorizationsTab = page.getByRole('tab', { name: 'Authorizations' });
        this.tenantTab = page.getByRole('link', { name: 'Tenants' });
        this.licenseKeyTagNonProduction = page
            .getByText('Non-production license')
            .first();
        this.licenseKeyTagNonCommercial = page.getByText('Non-commercial license');
        this.licenseKeyTagProduction = page.getByText('Production license').first();
        this.licenseKeyTagCommercial = page.getByText('Commercial license');
        this.zeebeCheckbox = page
            .locator('label')
            .filter({ hasText: 'Zeebe (Grants full access to the Zeebe API)' });
        this.identityCheckbox = page
            .locator('label')
            .filter({ hasText: 'Identity (Provides full access to Identity)' });
        this.modelerAdminCheckbox = page.locator('label').filter({
            hasText: 'Web Modeler Admin (Grants elevated access to Web Modeler)',
        });
    }
    async clickUsersLink() {
        try {
            await this.usersLink.click({ timeout: 180000, force: true });
        }
        catch {
            await this.page.reload();
            await this.usersLink.click({ timeout: 180000, force: true });
        }
    }
    async clickAssignedRolesTab() {
        await this.assignedRolesTab.click();
    }
    async clickDemoUser() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.demoUser, {
            visibilityTimeout: 15000,
            totalTimeout: 90000,
            postAction: async () => {
                await this.page.reload();
                await this.clickUsersLink();
            },
        });
        await this.demoUser.click();
    }
    async clickDeleteAccessButton(accessToDelete) {
        try {
            await this.page
                .locator('td:right-of(:text("' + accessToDelete + '"))')
                .first()
                .click({ timeout: 60000 });
        }
        catch {
            console.log('Access already deleted');
        }
    }
    async clickConfirmDeleteButton() {
        try {
            await this.confirmDeleteButton.click();
        }
        catch {
            console.log('Access already deleted');
        }
    }
    async clickAssignRolesButton() {
        await this.assignRolesButton.click();
    }
    async clickAddButton() {
        await this.addButton.click();
    }
    async clickOperateCheckbox() {
        await this.operateCheckbox.click();
    }
    async clickTasklistCheckbox() {
        await this.tasklistCheckbox.click();
    }
    async clickOptimizeCheckbox() {
        await this.optimizeCheckbox.click();
    }
    async clickModelerCheckbox() {
        await this.modelerCheckbox.click();
    }
    async clickConsoleCheckbox() {
        await this.consoleCheckbox.click();
    }
    async clickGroupsTab() {
        await this.groupsTab.click({ timeout: 120000 });
    }
    async clickCreateGroupButton() {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                if ((await this.noGoupsHeading.isVisible({ timeout: 120000 })) == false) {
                    await this.createGroupButton.click();
                }
                else {
                    await this.createFirstGroupButton.click();
                }
                return;
            }
            catch (error) {
                console.error(`Click attempt ${retries + 1} failed`);
            }
        }
        throw new Error(`Failed to click the button after ${maxRetries} attempts.`);
    }
    async fillGroupNameInput(name) {
        await this.groupNameInput.fill(name);
    }
    async clickUserGroup(name) {
        await this.page.getByRole('cell', { name: name }).click();
    }
    async clickAssignMembers() {
        await this.assignMembersButton.click();
    }
    async fillAssignMembers(names) {
        for (const name of names) {
            await this.assignMembersSearchbox.click();
            await this.assignMembersSearchbox.fill(name);
            await this.assignMembersSearchResult.filter({ hasText: name }).click();
        }
    }
    async clickAssignSubButton() {
        await this.assignMembersSubButton.click();
    }
    async clickCreateGroupSubButton() {
        await this.createGroupSubButton.click();
    }
    async deleteAllGroups() {
        try {
            await this.deleteButton
                .first()
                .waitFor({ state: 'visible', timeout: 120000 });
            const count = await this.deleteButton.count();
            if (count > 0) {
                for (let i = 0; i < count; i++) {
                    await this.deleteButton.nth(i).click({ timeout: 60000 });
                    await this.deleteSubButton.click({ timeout: 60000 });
                }
            }
        }
        catch (error) {
            console.log('Delete Button Not Clicked ' + error);
        }
    }
    async clickAuthorizationsTab() {
        await this.authorizationsTab.click();
    }
    async clickTenantTab() {
        await this.tenantTab.click();
    }
    async clickUser(username) {
        const searchbox = this.page.getByRole('searchbox');
        await searchbox.click();
        await searchbox.fill(username);
        const user = this.page.getByRole('cell', { name: username, exact: true });
        await user.click();
    }
    async clickZeebeCheckbox() {
        await this.zeebeCheckbox.click();
    }
    async clickIdentityCheckbox() {
        await this.identityCheckbox.click();
    }
    async clickModelerAdminCheckbox() {
        await this.modelerAdminCheckbox.click();
    }
}
exports.IdentityPage = IdentityPage;
