"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;
    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');
    }
    async connectorResultVariableName(name) {
        return await this.page.getByTestId(name);
    }
    async connectorResultVariableValue(variableName) {
        return await this.page.getByTestId(variableName).locator('td').last();
    }
    async incidentIconAssertion() {
        await (0, test_1.expect)(this.incidentIcon).toBeVisible({ timeout: 90000 });
    }
    async assertEitherIncidentOrActiveIconVisible() {
        return Promise.race([
            this.incidentIcon.waitFor({ timeout: 90000 }).then(() => 'incident'),
            this.activeIcon.waitFor({ timeout: 90000 }).then(() => 'active'),
        ]);
    }
    async assertEitherIncidentOrCompletedIconVisible() {
        return Promise.race([
            this.incidentIcon.waitFor({ timeout: 90000 }).then(() => 'incident'),
            this.completedIcon.waitFor({ timeout: 90000 }).then(() => 'completed'),
        ]);
    }
    async completedIconAssertion() {
        let retryCount = 0;
        const maxRetries = 3;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.completedIcon).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(`Completed 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 activeUserTaskIconVisibleAssertion(taskName) {
        let retryCount = 0;
        const maxRetries = 30;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(taskName)).toBeVisible({
                    timeout: 5000,
                });
                return;
            }
            catch (error) {
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async activeUserTaskIconNotVisibleAssertion(taskName) {
        let retryCount = 0;
        const maxRetries = 30;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(taskName)).not.toBeVisible({
                    timeout: 1000,
                });
                return;
            }
            catch (error) {
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
                await this.page.waitForLoadState('domcontentloaded');
            }
        }
        throw new Error(`Active icon visible after ${maxRetries} attempts.`);
    }
    async tenantNameAssertion(name) {
        await (0, test_1.expect)(this.page.getByRole('cell', { name: name, exact: true })).toBeVisible({ timeout: 60000 });
    }
    async assertTextVisible(text) {
        await (0, sleep_1.sleep)(2000); // moving too fast here means the text is not displayed.. clicking the row would fix this but there is no testId on the row so that would be flaky
        await this.page.reload();
        await (0, test_1.expect)(this.page.getByTestId('variables-list')).toBeVisible({
            timeout: 30000,
        });
        await (0, test_1.expect)(this.page.getByText(text)).toBeVisible({
            timeout: 60000,
        });
    }
}
exports.OperateProcessInstancePage = OperateProcessInstancePage;
