"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuditLogPage = void 0;
const test_1 = require("@playwright/test");
class AuditLogPage {
    auditLogTab;
    updateButton;
    clientOperationsSection;
    clientCategoryCheckbox;
    clientCategoryLabel;
    constructor(page) {
        this.auditLogTab = page.getByRole('tab', { name: 'Audit log' });
        const auditLogPanel = page.getByRole('tabpanel', { name: 'Audit log' });
        this.updateButton = auditLogPanel.getByRole('button', {
            name: 'Update',
            exact: true,
        });
        // Scope to the Client block: the category labels also exist under the User section.
        this.clientOperationsSection = auditLogPanel
            .getByText('Client operations to log', { exact: true })
            .locator('..');
        this.clientCategoryCheckbox = (category) => this.clientOperationsSection.getByRole('checkbox', { name: category });
        // Carbon hides the real <input>; toggle via the visible label.
        this.clientCategoryLabel = (category) => this.clientOperationsSection.locator('label').filter({ hasText: category });
    }
    async clickAuditLogTab() {
        await (0, test_1.expect)(this.auditLogTab).toBeVisible({ timeout: 60000 });
        await this.auditLogTab.click({ timeout: 60000 });
    }
    // Returns true when the category was newly checked (form made dirty).
    async ensureChecked(category) {
        const checkbox = this.clientCategoryCheckbox(category);
        await (0, test_1.expect)(checkbox).toBeAttached({ timeout: 30000 });
        if (await checkbox.isChecked()) {
            return false;
        }
        await this.clientCategoryLabel(category).click({ timeout: 30000 });
        await (0, test_1.expect)(checkbox).toBeChecked();
        return true;
    }
    async enableClientOperationLogging() {
        const categories = [
            'Administration',
            'Deployed resources',
            'User tasks',
        ];
        let changed = false;
        for (const category of categories) {
            if (await this.ensureChecked(category)) {
                changed = true;
            }
        }
        // Only persist when a toggle actually dirtied the form. Wait for the Update
        // button to enable rather than probing its state at a single instant —
        // Carbon enables it a tick after the last checkbox re-renders, so a bare
        // isEnabled() probe could skip the save and leave audit logging off.
        if (changed) {
            await (0, test_1.expect)(this.updateButton).toBeEnabled({ timeout: 60000 });
            await this.updateButton.click({ timeout: 60000 });
            await (0, test_1.expect)(this.updateButton).toBeDisabled({ timeout: 60000 });
        }
    }
}
exports.AuditLogPage = AuditLogPage;
