"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;
    incidentIcon;
    userTaskOpenTasklistLink;
    taskDetailsPopOver;
    connectorResultVariableName;
    variablesList;
    messageVariable;
    whatsNewPopUp;
    gotItButton;
    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.incidentIcon = page
            .getByTestId('instance-header')
            .getByTestId('INCIDENT-icon');
        this.userTaskOpenTasklistLink = page.getByRole('link', {
            name: 'Open Tasklist',
        });
        this.taskDetailsPopOver = page.getByTestId('popover');
        this.connectorResultVariableName = (name) => page.getByTestId('variable-' + name);
        this.variablesList = page.getByTestId('variables-list');
        this.messageVariable = page.getByTestId('variable-message');
        this.whatsNewPopUp = page.getByRole('heading', {
            name: "Here's what moved in Operate",
        });
        this.gotItButton = page.getByRole('button', { name: 'Got it' });
    }
    async connectorResultVariableValue(variableName) {
        return await this.page.getByTestId(variableName).locator('td').last();
    }
    async reload() {
        await this.page.reload();
    }
    async closePopOverIfVisible() {
        await (0, test_1.expect)(this.page.getByText('Instance History').first()).toBeVisible({
            timeout: 60000,
        });
        if (await this.whatsNewPopUp.isVisible()) {
            await this.gotItButton.click();
        }
    }
    async assertProcessCompleteStatusWithRetry(timeout = 60000, maxRetries = 10) {
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            try {
                await (0, test_1.expect)(this.completedIcon).toBeVisible({
                    timeout: timeout,
                });
                return;
            }
            catch (error) {
                if (attempt < maxRetries - 1) {
                    console.warn(`Process complete status attempt ${attempt + 1} failed. Retrying...`);
                    await this.page.reload();
                    await (0, sleep_1.sleep)(5000);
                }
                else {
                    throw new Error(`Assertion failed after ${maxRetries} attempts`);
                }
            }
        }
    }
    async assertProcessVariableContainsText(variableName, text) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(`variable-${variableName}`)).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} after ${maxRetries} attempts.`);
    }
    async clickDiagramTask(taskName) {
        const diagramTask = this.diagram.getByText(taskName);
        await (0, test_1.expect)(diagramTask).toBeVisible();
        await diagramTask.click();
    }
    async assertActiveTokenIsPresent() {
        await (0, test_1.expect)(this.activeIcon).toBeVisible({ timeout: 60000 });
    }
    async assertDetailsPopoverIsVisible() {
        await (0, test_1.expect)(this.taskDetailsPopOver).toBeVisible({
            timeout: 60000,
        });
    }
}
exports.OperateProcessInstancePage = OperateProcessInstancePage;
