"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OCIdentityClusterVariablesPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OCIdentityClusterVariablesPage {
    page;
    variablesList;
    editVariableButton;
    editVariableModal;
    variableValueField;
    saveVariableButton;
    closeEditVariableModal;
    cancelButton;
    variableRow;
    monacoEditor;
    monacoEditorTextArea;
    monacoSuggestWidget;
    successMessage;
    editMenuItem;
    editModalHeading;
    constructor(page) {
        this.page = page;
        this.variablesList = page.locator('table.cds--data-table');
        this.variableRow = (variableName) => this.variablesList
            .locator('tbody')
            .getByRole('row')
            .filter({ hasText: variableName });
        this.editVariableButton = (variableName) => this.variableRow(variableName).getByRole('button', { name: 'Options' });
        this.editVariableModal = page.getByRole('dialog', {
            name: /Edit.*variable/i,
        });
        this.variableValueField = this.editVariableModal.getByRole('textbox', {
            name: 'Editor content',
        });
        this.saveVariableButton = this.editVariableModal.locator('.cds--modal-footer button.cds--btn--primary');
        this.closeEditVariableModal = this.editVariableModal.getByRole('button', {
            name: 'Close',
        });
        this.cancelButton = this.editVariableModal.getByRole('button', {
            name: 'Cancel',
        });
        this.monacoEditor = this.editVariableModal.locator('.monaco-editor');
        this.monacoEditorTextArea = this.monacoEditor.locator('textarea.inputarea');
        this.monacoSuggestWidget = this.monacoEditor.locator('.editor-widget.suggest-widget');
        this.successMessage = this.page.getByText('Cluster variable updated');
        this.editModalHeading =
            this.editVariableModal.locator('.cds--modal-header');
        this.editMenuItem = this.page
            .locator('button[role="menuitem"]')
            .filter({ hasText: /^Edit$/ });
    }
    async editVariable(variableName, newValue) {
        await this.editVariableButton(variableName).click();
        await (0, test_1.expect)(this.editMenuItem).toBeVisible();
        await this.editMenuItem.click();
        await (0, test_1.expect)(this.variableValueField).toBeVisible();
        await (0, test_1.expect)(this.monacoEditor).toBeVisible();
        // Monaco editor doesn't respond reliably to clear()/fill() on the
        // hidden textarea. Use keyboard shortcuts to select-all and replace,
        // which mirrors real user interaction.
        // Retry the clear-type-enable cycle because Monaco auto-completion of
        // brackets / quotes can corrupt JSON input on the first attempt.
        const MAX_TYPE_ATTEMPTS = 3;
        for (let attempt = 1; attempt <= MAX_TYPE_ATTEMPTS; attempt++) {
            await this.monacoEditor.click();
            await this.monacoEditorTextArea.press('Control+a');
            await this.monacoEditorTextArea.press('Delete');
            // Let Monaco settle after clearing content
            await (0, sleep_1.sleep)(500);
            await this.monacoEditorTextArea.pressSequentially(newValue, { delay: 20 });
            // Dismiss any auto-complete popup by clicking the modal heading.
            // Do NOT use press('Escape') on the textarea — when no autocomplete
            // is showing, the Escape event propagates to the Carbon modal and
            // closes the entire dialog.
            await this.editModalHeading.click();
            // Let Monaco settle and validate the new content
            await (0, sleep_1.sleep)(500);
            try {
                await (0, test_1.expect)(this.saveVariableButton).toBeEnabled({ timeout: 10000 });
                break;
            }
            catch {
                if (attempt === MAX_TYPE_ATTEMPTS) {
                    throw new Error(`Save button did not become enabled after ${MAX_TYPE_ATTEMPTS} typing attempts`);
                }
            }
        }
        await this.saveVariableButton.click();
        await (0, test_1.expect)(this.successMessage).toBeVisible();
        await (0, test_1.expect)(this.editVariableModal).toBeHidden();
    }
    async assertVariableExists(variableName) {
        await (0, test_1.expect)(this.variableRow(variableName)).toBeVisible();
    }
    async assertVariableValue(variableName, expectedValue) {
        const row = this.variableRow(variableName);
        await (0, test_1.expect)(row).toContainText(expectedValue);
    }
}
exports.OCIdentityClusterVariablesPage = OCIdentityClusterVariablesPage;
