"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperateProcessesPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OperateProcessesPage {
    page;
    processResultCount;
    processActiveCheckbox;
    processCompletedCheckbox;
    processRunningInstancesCheckbox;
    processIncidentsCheckbox;
    processPageHeading;
    noMatchingInstancesMessage;
    processFinishedInstancesCheckbox;
    constructor(page) {
        this.page = page;
        this.processResultCount = page.getByTestId('result-count');
        this.processActiveCheckbox = page
            .locator('label')
            .filter({ hasText: 'Active' });
        this.processCompletedCheckbox = page
            .locator('label')
            .filter({ hasText: 'Completed' });
        this.processRunningInstancesCheckbox = page
            .locator('label')
            .filter({ hasText: 'Running Instances' });
        this.processIncidentsCheckbox = page
            .locator('label')
            .filter({ hasText: 'Incidents' });
        this.processPageHeading = page
            .getByTestId('expanded-panel')
            .getByRole('heading', { name: 'Process' });
        this.noMatchingInstancesMessage = page.getByText('There are no Instances matching this filter set');
        this.processFinishedInstancesCheckbox = page
            .getByTestId('filter-finished-instances')
            .getByRole('checkbox');
    }
    async clickProcessActiveCheckbox() {
        await this.processActiveCheckbox.click();
    }
    async clickProcessCompletedCheckbox() {
        await (0, sleep_1.sleep)(5000);
        await (0, test_1.expect)(this.processCompletedCheckbox).toBeVisible({
            timeout: 30000,
        });
        await this.processCompletedCheckbox.check();
        await (0, sleep_1.sleep)(2000);
    }
    async clickProcessIncidentsCheckbox() {
        await this.processIncidentsCheckbox.click({ timeout: 90000 });
    }
    async clickRunningProcessInstancesCheckbox() {
        await this.processRunningInstancesCheckbox.click({ timeout: 90000 });
    }
    async clickFinishedProcessInstancesCheckbox() {
        await this.processFinishedInstancesCheckbox.click({ timeout: 90000 });
    }
    async clickProcessInstanceLink(processName) {
        let retryCount = 0;
        const maxRetries = 3;
        while (retryCount < maxRetries) {
            try {
                await this.page.reload();
                await this.page
                    .getByRole('row')
                    .filter({ hasText: new RegExp(`^Select row${processName}\\d+.*$`) })
                    .first()
                    .getByRole('link')
                    .click({ timeout: 30000 });
                return; // Exit the function if the click is successful
            }
            catch (error) {
                // If process 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(`Failed to click on process instance link after ${maxRetries} attempts.`);
    }
}
exports.OperateProcessesPage = OperateProcessesPage;
