"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();
        // Carbon's owner combobox is a Downshift-driven ComboBox. The form's
        // Resource type combobox is gated on Downshift's `selectedItem`
        // state, NOT on the input's `value` attribute. To commit
        // `selectedItem` we must click the active menu item — i.e. the
        // element with class `.cds--list-box__menu-item` inside the open
        // dropdown.
        //
        // The previous `getByRole('option', {name, exact: true})` locator
        // matched a role=option element but the click did not fire
        // Downshift's `onSelectedItemChange` (frame snapshot from run
        // 26632778543 shows the input value populated but Resource type
        // still disabled). Scoping to the Carbon menu-item class — the same
        // approach used in the product repo's qa-suite
        // (camunda/camunda qa/c8-orchestration-cluster-e2e-test-suite/
        // pages/IdentityAuthorizationsPage.ts selectAuthorizationOwner) —
        // selects the actual Downshift menu item and fires the handler.
        const ownerSearchInput = this.createAuthorizationModal.getByPlaceholder('Search by owner ID');
        if (await ownerSearchInput.isVisible({ timeout: 5000 }).catch(() => false)) {
            await ownerSearchInput.fill(authorization.ownerId);
        }
        const ownerMenuItem = this.createAuthorizationModal
            .locator('.cds--list-box__menu-item')
            .filter({ hasText: authorization.ownerId })
            .first();
        await (0, test_1.expect)(ownerMenuItem).toBeVisible({ timeout: 60000 });
        await ownerMenuItem.click({ timeout: 20000 });
        // Resource type is pre-filled and locked starting with the Identity
        // commit camunda/camunda 8c72d29 (2026-05-26, "fix: prefill and lock
        // resource type in create authorization modal"). The modal now
        // initializes `resourceType` to whatever tab the user opened the
        // modal from (the test calls clickResourceTab/clickComponentTab/etc.
        // before createAuthorization), and the Dropdown is rendered with a
        // hardcoded `disabled` prop — see
        // identity/client/src/pages/authorizations/modals/add-modal/AddModal.tsx
        // line ~184. There is no test-side interaction that can enable it,
        // and no interaction is needed: the form already has the right
        // value. Skip the dropdown click entirely. (8.9 still uses the older
        // unlocked Dropdown, which is why its same-shaped page object
        // continues to pass.)
        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();
        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;
