"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')
            .getByText('Process', { exact: true });
        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 this.processCompletedCheckbox.click({ timeout: 30000 });
        await (0, sleep_1.sleep)(1000);
    }
    async clickProcessIncidentsCheckbox() {
        await this.processIncidentsCheckbox.click();
    }
    async clickRunningProcessInstancesCheckbox() {
        await this.processRunningInstancesCheckbox.click();
    }
    async clickFinishedProcessInstancesCheckbox() {
        await this.processFinishedInstancesCheckbox.click();
    }
    async clickProcessInstanceLink(processName) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                const process = this.page
                    .locator('td:right-of(:text("' + processName + '"))')
                    .first();
                await (0, test_1.expect)(process).toBeVisible({ timeout: 60000 });
                await process.click({ timeout: 60000 });
                await (0, test_1.expect)(this.page.getByText('Instance History').first()).toBeVisible();
                return;
            }
            catch (error) {
                console.error(`Click attempt ${retries + 1} failed: ${error}`);
                await new Promise((resolve) => setTimeout(resolve, 10000));
                await this.page.reload();
            }
        }
        throw new Error(`Failed to click the link after ${maxRetries} attempts.`);
    }
    async reload() {
        await this.page.reload();
    }
}
exports.OperateProcessesPage = OperateProcessesPage;
