"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;
    noMatchingInstancesMessage;
    processFinishedInstancesCheckbox;
    processNameFilteringPlaceHolder;
    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.noMatchingInstancesMessage = page.getByText('There are no Instances matching this filter set');
        this.processFinishedInstancesCheckbox = page
            .getByTestId('filter-finished-instances')
            .getByRole('checkbox');
        this.processNameFilteringPlaceHolder = this.page.getByPlaceholder('Search by Process Name');
    }
    async clickProcessActiveCheckbox() {
        await this.processActiveCheckbox.click();
    }
    async clickProcessCompletedCheckbox() {
        await (0, sleep_1.sleep)(5000);
        if (!(await this.processCompletedCheckbox.isChecked({ timeout: 60000 }))) {
            await this.processCompletedCheckbox.click({ timeout: 120000 });
            await (0, sleep_1.sleep)(1000);
        }
    }
    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 fillProcessNameFilter(processName) {
        await this.processNameFilteringPlaceHolder.click({ timeout: 30000 });
        await this.processNameFilteringPlaceHolder.fill(processName);
        await this.processNameFilteringPlaceHolder.press('Enter');
    }
    async clickProcessInstanceLink(processName) {
        let retryCount = 0;
        const maxRetries = 5;
        while (retryCount < maxRetries) {
            try {
                const link = this.page
                    .getByRole('row')
                    .filter({ hasText: new RegExp(`^Select row${processName}\\d+.*$`) })
                    .first()
                    .getByRole('link');
                await (0, test_1.expect)(link).toBeVisible({ timeout: 20000 });
                await link.click({ timeout: 30000 });
                return;
            }
            catch (error) {
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...` + error);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
                if (retryCount >= 3) {
                    await this.fillProcessNameFilter(processName);
                }
            }
        }
        throw new Error(`Failed to click on process instance link after ${maxRetries} attempts.`);
    }
}
exports.OperateProcessesPage = OperateProcessesPage;
