"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperateHomePage = void 0;
const test_1 = require("@playwright/test");
class OperateHomePage {
    page;
    operateBanner;
    processesTab;
    informationDialog;
    editVariableButton;
    variableValueInput;
    saveVariableButton;
    editVariableSpinner;
    constructor(page) {
        this.page = page;
        this.operateBanner = page.getByRole('link', { name: 'Camunda logo Operate' });
        this.processesTab = page.getByRole('link', { name: 'Processes' });
        this.informationDialog = page.getByRole('button', {
            name: 'Close this dialog',
        });
        this.editVariableButton = page.getByTestId('edit-variable-button');
        this.variableValueInput = page.getByTestId('edit-variable-value');
        // Carbon icon-only buttons expose their accessible name via aria-labelledby
        // (tooltip text "Save") which overrides aria-label for computed accessible name.
        // Use a CSS attribute selector to match the raw aria-label attribute directly.
        this.saveVariableButton = page.locator('[aria-label="Save variable"]');
        this.editVariableSpinner = page
            .getByTestId('variable-operation-spinner')
            .locator('circle')
            .nth(1);
    }
    async operateBannerIsVisible() {
        await (0, test_1.expect)(this.operateBanner).toBeVisible({ timeout: 30000 });
    }
    async clickProcessesTab() {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                await (0, test_1.expect)(this.processesTab).toBeVisible({ timeout: 30000 });
                await this.processesTab.click({ timeout: 30000 });
                await (0, test_1.expect)(this.page.locator('label').filter({ hasText: 'Completed' }).first()).toBeVisible();
                return;
            }
            catch (error) {
                console.warn(`Click attempt ${retries + 1} failed: ${error}`);
            }
        }
        throw new Error(`Failed to click the processes tab after ${maxRetries} attempts.`);
    }
    async clickEditVariableButton(variableName) {
        const editVariableButton = 'variable-' + variableName;
        await this.page.getByTestId(editVariableButton).getByLabel('Edit').click();
    }
    async clickVariableValueInput() {
        await this.variableValueInput.click();
    }
    async clearVariableValueInput() {
        await this.variableValueInput.clear();
    }
    async fillVariableValueInput(newValue) {
        // clickEditVariableButton triggers autoFocus → editor.focus() in Monaco's
        // onMount. Wait for the cursor to confirm Monaco has keyboard focus before
        // sending events. Scoped to the variable row to avoid matching other editors.
        await this.page
            .getByTestId('variable-testVariable')
            .locator('.monaco-editor .cursor')
            .first()
            .waitFor({ state: 'visible', timeout: 30000 });
        // Select all, then replace with insertText (single InputEvent dispatch).
        // keyboard.type() triggers Monaco's auto-close-quote feature which inserts
        // an extra closing quote and produces invalid JSON. insertText dispatches one
        // input event that replaces the selection atomically, bypassing auto-close.
        await this.page.keyboard.press('Control+A');
        await this.page.keyboard.insertText(`"${newValue}`);
    }
    async clickSaveVariableButton() {
        await this.saveVariableButton.click({ timeout: 30000 });
    }
}
exports.OperateHomePage = OperateHomePage;
