"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperateProcessInstancePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OperateProcessInstancePage {
    page;
    diagram;
    completedIcon;
    diagramSpinner;
    activeIcon;
    variablesList;
    messageVariable;
    statusVariable;
    constructor(page) {
        this.page = page;
        this.diagram = page.getByTestId('diagram');
        this.completedIcon = page
            .getByTestId('instance-header')
            .getByTestId('COMPLETED-icon');
        this.diagramSpinner = page.getByTestId('diagram-spinner');
        this.activeIcon = page
            .getByTestId('instance-header')
            .getByTestId('ACTIVE-icon');
        this.variablesList = page.getByTestId('variables-list');
        this.messageVariable = page.getByTestId('variable-message');
        this.statusVariable = page.getByTestId('variable-status');
    }
    async connectorResultVariableName(name) {
        return await this.page.getByTestId(name);
    }
    async connectorResultVariableValue(variableName) {
        return await this.page.getByTestId(variableName).locator('td').last();
    }
    async getProcessVariableValue(variableName) {
        return await this.page
            .getByTestId(`variable-${variableName}`)
            .getByRole('cell')
            .nth(1);
    }
    async completedIconAssertion() {
        let retryCount = 0;
        const maxRetries = 6;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.completedIcon).toBeVisible({
                    timeout: 60000,
                });
                return; // Exit the function if the expectation is met
            }
            catch (error) {
                // If the active icon isn't found, reload the page and try again
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async activeIconAssertion() {
        let retryCount = 0;
        const maxRetries = 3;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.activeIcon).toBeVisible({
                    timeout: 90000,
                });
                return; // Exit the function if the expectation is met
            }
            catch (error) {
                // If the active icon isn't found, reload the page and try again
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async assertProcessVariableContainsText(variableName, text) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                const valueCell = await this.getProcessVariableValue(variableName);
                await (0, test_1.expect)(valueCell).toContainText(text, { timeout: 30000 });
                return;
            }
            catch (error) {
                console.log(`Failed to assert variable ${variableName}: ${error}`);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Failed to assert variable ${variableName} contains ${text} after ${maxRetries} attempts.`);
    }
    /**
     * Gets the JSON value of a variable by name
     */
    async getVariableJsonValue(variableName) {
        const variable = await this.getProcessVariableValue(variableName);
        await (0, test_1.expect)(variable).toBeVisible();
        const valueText = (await variable.textContent()) || '{}';
        try {
            return JSON.parse(valueText);
        }
        catch (error) {
            throw new Error(`Failed to parse JSON for variable "${variableName}". ` +
                `Raw value: ${valueText}. Original error: ${String(error)}`);
        }
    }
    /**
     * Verifies that a variable exists and contains expected JSON content
     */
    async verifyVariableJsonContent(variableName, assertions) {
        const value = await this.getVariableJsonValue(variableName);
        await assertions(value);
    }
}
exports.OperateProcessInstancePage = OperateProcessInstancePage;
