"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdentityHomePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
class IdentityHomePage {
    page;
    identityBanner;
    tasklistCell;
    operateCell;
    connectorsCell;
    accessToAPIsTab;
    writePermissionCell;
    readPermissionCell;
    assignPermissionsButton;
    dialog;
    writePermissionCheckBox;
    readPermissionCheckBox;
    addButton;
    permissionCell;
    select;
    constructor(page) {
        this.page = page;
        this.identityBanner = page.getByRole('link', {
            name: 'Camunda logo Identity',
        });
        this.accessToAPIsTab = page.getByRole('tab', { name: 'Access to APIs' });
        this.writePermissionCell = page.getByRole('cell', { name: 'write:*' });
        this.readPermissionCell = page.getByRole('cell', { name: 'read:*' });
        this.assignPermissionsButton = page.getByRole('button', {
            name: 'Assign permissions',
        });
        this.dialog = page.getByRole('dialog');
        this.writePermissionCheckBox = page
            .locator('label')
            .filter({ hasText: 'write:*' });
        this.readPermissionCheckBox = page
            .locator('label')
            .filter({ hasText: 'read:*' });
        this.addButton = page.getByRole('button', { name: 'Add' });
        this.permissionCell = (name) => page.getByRole('cell', { name: name });
        this.tasklistCell = this.permissionCell('Tasklist');
        this.operateCell = this.permissionCell('Operate');
        this.connectorsCell = this.permissionCell('Connectors');
        this.select = this.page.locator('select');
    }
    async assignPermissionToTasklistAPI() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.tasklistCell);
        await this.tasklistCell.click();
        await this.accessToAPIsTab.click();
        await (0, sleep_1.sleep)(2000);
        if ((await this.writePermissionCell.isVisible()) &&
            (await this.readPermissionCell.isVisible())) {
            console.log('Permissions already assigned to Tasklist API');
        }
        else {
            await this.assignPermissionsButton.click();
            await this.selectOptionFromDropdown('Tasklist API');
            await (0, test_1.expect)(this.dialog).toBeVisible();
            await this.writePermissionCheckBox.waitFor({ state: 'visible' });
            await this.writePermissionCheckBox.click();
            await this.readPermissionCheckBox.click();
            await this.addButton.click();
            await (0, test_1.expect)(this.permissionCell('write:*')).toBeVisible();
            await (0, test_1.expect)(this.permissionCell('read:*')).toBeVisible();
            await (0, sleep_1.sleep)(5000);
        }
    }
    async assignPermissionToOperateAPI() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.operateCell);
        await this.operateCell.click();
        await this.accessToAPIsTab.click();
        await (0, sleep_1.sleep)(2000);
        if ((await this.writePermissionCell.isVisible()) &&
            (await this.readPermissionCell.isVisible())) {
            console.log('Permissions already assigned to Operate API');
        }
        else {
            await this.assignPermissionsButton.click();
            await this.selectOptionFromDropdown('Operate API');
            await (0, test_1.expect)(this.dialog).toBeVisible();
            await this.writePermissionCheckBox.waitFor({ state: 'visible' });
            await this.writePermissionCheckBox.click();
            await this.readPermissionCheckBox.click();
            await this.addButton.click();
            await (0, test_1.expect)(this.permissionCell('write:*')).toBeVisible();
            await (0, test_1.expect)(this.permissionCell('read:*')).toBeVisible();
            await (0, sleep_1.sleep)(5000);
        }
    }
    async selectOptionFromDropdown(option) {
        await this.select.click();
        await this.select.selectOption({
            label: option,
        });
        await (0, sleep_1.sleep)(1000);
    }
}
exports.IdentityHomePage = IdentityHomePage;
