"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Authorization = void 0;
const test_1 = require("@playwright/test");
const UtilitiesPage_1 = require("../SM-8.8/UtilitiesPage");
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.getByRole('checkbox', {
            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,
        });
        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 this.createAuthorizationAccessPermission(permission).click({
                force: true,
            });
        }
        await this.createAuthorizationSubmitButton.click();
        await (0, test_1.expect)(this.createAuthorizationModal).not.toBeVisible();
        // Match the created authorization row by chaining filters on owner ID and
        // resource ID. A single concatenated `hasText` does not match because
        // Playwright whitespace-normalizes cell text and the rendered table
        // separates adjacent cells with whitespace (e.g. tab characters).
        const item = this.page
            .getByRole('row')
            .filter({ hasText: authorization.ownerId })
            .filter({ hasText: authorization.resourceId });
        // Retry to handle eventual consistency after the submit. We cannot use the
        // shared `assertLocatorVisibleWithRetry` helper here because the row may
        // be on a later pagination page (Carbon DataTable does not render rows
        // outside the current page), so we have to narrow the visible result set
        // by typing the owner ID into the table search input (placeholder reads
        // "Search by owner ID"). The filter input is cleared on every reload so we
        // re-apply it on each attempt.
        const maxAttempts = 10;
        let lastError;
        for (let attempt = 0; attempt < maxAttempts; attempt++) {
            try {
                await this.selectResourceTypeTab(authorization.resourceType).click();
                await this.tableFilter.click();
                await this.tableFilter.fill(authorization.ownerId);
                await (0, test_1.expect)(item).toBeVisible({ timeout: 5000 });
                return;
            }
            catch (error) {
                lastError = error;
                console.warn(`Attempt ${attempt + 1}/${maxAttempts} failed asserting ${authorization.resourceType} authorization row (${authorization.ownerId} / ${authorization.resourceId}). Retrying...`);
                if (attempt < maxAttempts - 1) {
                    await this.page.reload();
                }
            }
        }
        throw new Error(`Authorization row not visible after ${maxAttempts} attempts: ${authorization.ownerId} / ${authorization.resourceId} (${authorization.resourceType}). Last error: ${lastError}`);
    }
    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;
