"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Authorization = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../SM-8.10/UtilitiesPage");
const submitModalWithRetry_1 = require("../../utils/assertionHelpers/submitModalWithRetry");
const sleep_1 = require("../../utils/sleep");
class Authorization {
    page;
    selectResourceTypeTab;
    createAuthorizationModal;
    createAuthorizationButton;
    createAuthorizationOwnerTypeCombobox;
    createAuthorizationOwnerTypeOption;
    createAuthorizationOwnerComboBox;
    createAuthorizationOwnerOption;
    createAuthorizationResourceTypeDropdown;
    createAuthorizationResourceTypeOption;
    createAuthorizationResourceIdField;
    createAuthorizationAccessPermission;
    createAuthorizationSubmitButton;
    authorizationsList;
    deleteAuthorizationButton;
    deleteAuthorizationModal;
    deleteAuthorizationModalDeleteButton;
    tableFilter;
    constructor(page) {
        this.page = page;
        this.selectResourceTypeTab = (resourceType) => page.getByRole('tab', { name: resourceType });
        this.authorizationsList = page.getByRole('table');
        this.createAuthorizationButton = page.getByRole('button', {
            name: /create authorization/i,
        });
        this.createAuthorizationModal = page.getByRole('dialog', {
            name: 'Create authorization',
        });
        this.createAuthorizationOwnerTypeCombobox =
            this.createAuthorizationModal.getByRole('combobox', {
                name: 'Owner type',
            });
        this.createAuthorizationOwnerTypeOption = (name) => this.createAuthorizationModal.getByRole('option', {
            name,
            exact: true,
        });
        this.createAuthorizationOwnerComboBox =
            this.createAuthorizationModal.getByRole('combobox', {
                name: 'Owner',
                exact: true,
            });
        this.createAuthorizationOwnerOption = (name) => this.createAuthorizationModal.getByRole('option', {
            name,
            exact: true,
        });
        this.createAuthorizationResourceTypeDropdown =
            this.createAuthorizationModal.getByRole('combobox', {
                name: 'Resource type',
            });
        this.createAuthorizationResourceTypeOption = (name) => this.createAuthorizationModal.getByRole('option', {
            name,
            exact: true,
        });
        this.createAuthorizationResourceIdField =
            this.createAuthorizationModal.getByRole('textbox', {
                name: 'Resource ID',
            });
        this.createAuthorizationAccessPermission = (name) => this.createAuthorizationModal.locator(`.cds--checkbox-wrapper label[for="${name}"]`);
        this.createAuthorizationSubmitButton =
            this.createAuthorizationModal.getByRole('button', {
                name: 'Create authorization',
            });
        this.authorizationsList = page.getByRole('table');
        this.deleteAuthorizationButton = (name) => this.authorizationsList.getByRole('row', { name }).getByLabel('Delete');
        this.deleteAuthorizationModal = page.getByRole('dialog', {
            name: /delete authorization/i,
        });
        this.deleteAuthorizationModalDeleteButton =
            this.deleteAuthorizationModal.getByRole('button', {
                name: /delete authorization/i,
            });
        this.tableFilter = page.getByRole('searchbox', { name: 'Filter table' });
    }
    async createAuthorization(authorization) {
        await this.createAuthorizationButton.click();
        await (0, test_1.expect)(this.createAuthorizationModal).toBeVisible();
        await this.createAuthorizationOwnerTypeCombobox.click();
        await this.createAuthorizationOwnerTypeOption(authorization.ownerType).click();
        await this.createAuthorizationOwnerComboBox.click();
        await (0, test_1.expect)(this.createAuthorizationOwnerOption(authorization.ownerId)).toBeVisible({
            timeout: 30000,
        });
        await this.createAuthorizationOwnerOption(authorization.ownerId).click({
            timeout: 30000,
        });
        // The Resource type is pre-selected and disabled when the dialog is
        // opened from a specific resource-type tab. Only pick it from the dropdown
        // when the field is still enabled (editable).
        await (0, test_1.expect)(this.createAuthorizationResourceTypeDropdown).toBeVisible({
            timeout: 30000,
        });
        if (await this.createAuthorizationResourceTypeDropdown.isEnabled()) {
            await this.createAuthorizationResourceTypeDropdown.click();
            await this.createAuthorizationResourceTypeOption(authorization.resourceType).click({ timeout: 30000 });
        }
        await this.createAuthorizationResourceIdField.fill(authorization.resourceId);
        for (const permission of authorization.accessPermissions) {
            await (0, sleep_1.sleep)(200);
            const permissionLabel = this.createAuthorizationAccessPermission(permission);
            const permissionInput = this.createAuthorizationModal.locator(`input[id="${permission}"]`);
            await (0, test_1.expect)(permissionLabel).toBeVisible();
            if (!(await permissionInput.isChecked())) {
                await permissionLabel.scrollIntoViewIfNeeded();
                await (0, sleep_1.sleep)(300);
                let clickAttempts = 0;
                const maxAttempts = 3;
                while (!(await permissionInput.isChecked()) &&
                    clickAttempts < maxAttempts) {
                    clickAttempts++;
                    await permissionLabel.click({ force: true });
                    await (0, sleep_1.sleep)(500);
                    if (await permissionInput.isChecked()) {
                        break;
                    }
                    if (clickAttempts < maxAttempts) {
                        await permissionLabel.click();
                        await (0, sleep_1.sleep)(300);
                    }
                }
                await (0, test_1.expect)(permissionInput).toBeChecked({ timeout: 3000 });
            }
        }
        await (0, submitModalWithRetry_1.submitModalWithRetry)(this.createAuthorizationSubmitButton, this.createAuthorizationModal);
        await this.selectResourceTypeTab(authorization.resourceType).click();
        const item = this.page.getByRole('row').filter({
            hasText: `${authorization.ownerId}${authorization.resourceId}`,
        });
        await this.tableFilter.click();
        await this.tableFilter.fill(authorization.resourceType);
        await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(this.page, item, authorization.resourceType, 5000, false, 10, this.selectResourceTypeTab(authorization.resourceType));
    }
    async deleteAuthorization(resourceId, resourceType) {
        await this.deleteAuthorizationButton(resourceId).click();
        await (0, test_1.expect)(this.deleteAuthorizationModal).toBeVisible();
        await this.deleteAuthorizationModalDeleteButton.click();
        await (0, test_1.expect)(this.deleteAuthorizationModal).not.toBeVisible();
        await (0, UtilitiesPage_1.assertLocatorVisibleWithRetry)(this.page, this.authorizationsList
            .getByRole('cell', {
            name: resourceId,
        })
            .first(), resourceId, 5000, true, 3, this.selectResourceTypeTab(resourceType));
    }
}
exports.Authorization = Authorization;
