"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Authorization = void 0;
const test_1 = require("@playwright/test");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
const expectLocatorWithPagination_1 = require("../../utils/assertionHelpers/expectLocatorWithPagination");
class Authorization {
    page;
    selectResourceTypeTab;
    createAuthorizationModal;
    createAuthorizationButton;
    createAuthorizationOwnerTypeCombobox;
    createAuthorizationOwnerTypeOption;
    createAuthorizationOwnerComboBox;
    createAuthorizationOwnerOption;
    createAuthorizationResourceTypeDropdown;
    createAuthorizationResourceTypeOption;
    createAuthorizationResourceIdField;
    createAuthorizationAccessPermission;
    createAuthorizationSubmitButton;
    authorizationsList;
    deleteAuthorizationButton;
    deleteAuthorizationModal;
    deleteAuthorizationModalDeleteButton;
    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.getByRole('checkbox', {
            name,
        });
        this.createAuthorizationSubmitButton =
            this.createAuthorizationModal.getByRole('button', {
                name: 'Create authorization',
            });
        this.authorizationsList = page.getByRole('table');
        this.deleteAuthorizationButton = (name) => this.authorizationsList
            .getByRole('row')
            .filter({ hasText: name })
            .first()
            .getByRole('button', { name: 'danger Delete' });
        this.deleteAuthorizationModal = page.getByRole('dialog', {
            name: /delete authorization/i,
        });
        this.deleteAuthorizationModalDeleteButton =
            this.deleteAuthorizationModal.getByRole('button', {
                name: /delete authorization/i,
            });
    }
    async assertAuthorization(auth) {
        await (0, test_1.expect)(this.authorizationsList.getByRole('row').filter({ hasText: auth }).first()).toBeVisible({ timeout: 30000 });
    }
    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,
        });
        await this.createAuthorizationResourceTypeDropdown.click();
        await this.createAuthorizationResourceTypeOption(authorization.resourceType).click({ timeout: 30000, force: true });
        await this.createAuthorizationResourceIdField.fill(authorization.resourceId);
        for (const permission of authorization.accessPermissions) {
            await this.createAuthorizationAccessPermission(permission).click({
                force: true,
            });
        }
        await this.createAuthorizationSubmitButton.click();
        await (0, test_1.expect)(this.createAuthorizationModal).not.toBeVisible({
            timeout: 30000,
        });
        await this.selectResourceTypeTab(authorization.resourceType).click();
        const item = this.page.getByRole('row').filter({
            hasText: `${authorization.ownerId}${authorization.resourceId}`,
        });
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, item, {
            clickLocator: this.selectResourceTypeTab(authorization.resourceType),
        });
    }
    async deleteAuthorization(resourceId, resourceType) {
        const locator = this.deleteAuthorizationButton(resourceId);
        await (0, expectLocatorWithPagination_1.expectLocatorWithPagination)(this.page, locator, {
            clickLocator: this.selectResourceTypeTab(resourceType),
        });
        await locator.click();
        await (0, test_1.expect)(this.deleteAuthorizationModal).toBeVisible();
        await this.deleteAuthorizationModalDeleteButton.click();
        await (0, test_1.expect)(this.deleteAuthorizationModal).not.toBeVisible({
            timeout: 30000,
        });
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.authorizationsList
            .getByRole('cell', {
            name: resourceId,
        })
            .first(), {
            preAction: async () => {
                await this.selectResourceTypeTab(resourceType);
                await this.page.reload();
                await this.page.waitForTimeout(1000);
            },
            shouldBeVisible: false,
        });
    }
}
exports.Authorization = Authorization;
