"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;
    variablesList;
    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.variablesList = page.getByTestId('variables-list');
    }
    async assertVariablesListVisible(timeout = 30000) {
        const startTime = Date.now();
        while (Date.now() - startTime < timeout) {
            try {
                await (0, test_1.expect)(this.variablesList).toBeVisible({ timeout: 10000 });
                return;
            }
            catch {
                console.log(`Variables list not visible, reloading... (${Math.round((Date.now() - startTime) / 1000)}s elapsed)`);
                await this.page
                    .reload({ timeout: 10000 })
                    .catch(() => console.log('Page reload timed out, continuing...'));
                await (0, sleep_1.sleep)(2000);
            }
        }
        throw new Error(`Variables list not visible after ${Math.round(timeout / 1000)}s`);
    }
    async connectorResultVariableName(name, timeout = 30000) {
        const locator = this.page.getByTestId('variable-' + name);
        const startTime = Date.now();
        while (Date.now() - startTime < timeout) {
            try {
                await (0, test_1.expect)(locator).toBeVisible({ timeout: 10000 });
                return locator;
            }
            catch {
                console.log(`Variable "${name}" not visible, reloading... (${Math.round((Date.now() - startTime) / 1000)}s elapsed)`);
                await this.page
                    .reload({ timeout: 10000 })
                    .catch(() => console.log('Page reload timed out, continuing...'));
                await (0, sleep_1.sleep)(2000);
            }
        }
        throw new Error(`Variable "${name}" not visible after ${Math.round(timeout / 1000)}s`);
    }
    async connectorResultVariableValue(variableName, timeout = 30000) {
        const locator = this.page.getByTestId('variable-' + variableName);
        const startTime = Date.now();
        while (Date.now() - startTime < timeout) {
            try {
                await (0, test_1.expect)(locator).toBeVisible({ timeout: 10000 });
                return locator;
            }
            catch {
                console.log(`Variable value "${variableName}" not visible, reloading... (${Math.round((Date.now() - startTime) / 1000)}s elapsed)`);
                await this.page
                    .reload({ timeout: 10000 })
                    .catch(() => console.log('Page reload timed out, continuing...'));
                await (0, sleep_1.sleep)(2000);
            }
        }
        throw new Error(`Variable value "${variableName}" not visible after ${Math.round(timeout / 1000)}s`);
    }
    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 this.page.waitForLoadState('domcontentloaded');
            }
        }
        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 this.page.waitForLoadState('domcontentloaded');
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async activeUserTaskIconVisibleAssertion(taskName) {
        let retryCount = 0;
        const maxRetries = 3;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(taskName)).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 this.page.waitForLoadState('domcontentloaded');
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async activeUserTaskIconNotVisibleAssertion(taskName) {
        let retryCount = 0;
        const maxRetries = 3;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(taskName)).not.toBeVisible({
                    timeout: 90000,
                });
                return; // Exit the function if the expectation is met
            }
            catch (error) {
                // If the active icon is found, reload the page and try again
                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 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 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 this.page.waitForLoadState('domcontentloaded');
                }
                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 this.page.waitForLoadState('domcontentloaded');
            }
        }
        throw new Error(`Failed to assert variable ${variableName} after ${maxRetries} attempts.`);
    }
    async assertActiveTokenIsPresent() {
        await (0, test_1.expect)(this.activeIcon).toBeVisible({ timeout: 60000 });
    }
}
exports.OperateProcessInstancePage = OperateProcessInstancePage;
