"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;
    processInstanceLink;
    moreFiltersButton;
    processInstanceKeyTextBox;
    whatsNewPopUp;
    gotItButton;
    constructor(page) {
        this.page = page;
        this.processResultCount = page.getByTestId('result-count');
        this.processInstanceLink = (name) => this.page
            .getByRole('row')
            .filter({ hasText: name })
            .first()
            .getByRole('link', { name: /^View instance \d+/ });
        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');
        this.moreFiltersButton = page.getByRole('button', { name: 'More filters' });
        this.processInstanceKeyTextBox = page
            .locator('#processInstanceKey')
            .or(page.locator('#ids'));
        this.whatsNewPopUp = page.getByRole('heading', {
            name: "Here's what moved in Operate",
        });
        this.gotItButton = page.getByRole('button', { name: 'Got it' });
    }
    getMoreFilterValueLocator(filter) {
        switch (filter.trim()) {
            case 'Process Instance Key(s)':
                return this.processInstanceKeyTextBox;
            default:
                throw new Error(`Unsupported filter: "${filter}". Add a mapping in getMoreFilterValueLocator().`);
        }
    }
    async clickProcessActiveCheckbox() {
        await this.processActiveCheckbox.click();
    }
    async clickProcessCompletedCheckbox() {
        await this.processCompletedCheckbox.click({ timeout: 60000 });
        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 {
                await (0, test_1.expect)(this.processInstanceLink(processName)).toBeVisible({
                    timeout: 60000,
                });
                await this.processInstanceLink(processName).click({ timeout: 60000 });
                await (0, test_1.expect)(this.page.getByText('Instance History').first()).toBeVisible({
                    timeout: 10000,
                });
                if (await this.whatsNewPopUp.isVisible()) {
                    await this.gotItButton.click();
                }
                return;
            }
            catch (error) {
                console.error(`Click attempt ${retries + 1} failed: ${error}`);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Failed to click the link after ${maxRetries} attempts.`);
    }
    async applyMoreFilters(filter, value) {
        await this.moreFiltersButton.click();
        await this.page.getByRole('menuitem', { name: filter }).click();
        const valueInput = this.getMoreFilterValueLocator(filter);
        await (0, test_1.expect)(valueInput).toBeVisible({ timeout: 10000 });
        await valueInput.fill(value);
        await (0, sleep_1.sleep)(2000);
    }
}
exports.OperateProcessesPage = OperateProcessesPage;
