"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperateProcessesPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const constants_1 = require("../../utils/constants");
class OperateProcessesPage {
    page;
    processResultCount;
    processActiveCheckbox;
    processCompletedCheckbox;
    processRunningInstancesCheckbox;
    processIncidentsCheckbox;
    processPageHeading;
    noMatchingInstancesMessage;
    processFinishedInstancesCheckbox;
    tenantDropdown;
    tenantDropdownOption;
    tenantComboBox;
    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');
        this.tenantComboBox = page.getByRole('combobox', { name: 'Tenant' });
        this.tenantDropdown = page.getByRole('combobox', { name: 'Tenant' });
        this.tenantDropdownOption = (name) => page.getByRole('option', { name: name });
    }
    async checkCheckbox(checkbox) {
        await checkbox.waitFor({ state: 'visible', timeout: constants_1._1_SECOND_IN_MS * 10 });
        if (!(await checkbox.isChecked())) {
            await checkbox.click();
            await (0, test_1.expect)(checkbox).toBeChecked({
                checked: true,
                timeout: constants_1._1_SECOND_IN_MS * 2,
            });
        }
    }
    async uncheckCheckbox(checkbox) {
        if (await checkbox.isChecked()) {
            await checkbox.click();
            await (0, test_1.expect)(checkbox).toBeChecked({
                checked: false,
                timeout: constants_1._1_SECOND_IN_MS * 2,
            });
        }
    }
    async clickProcessActiveCheckbox() {
        await this.checkCheckbox(this.processActiveCheckbox);
    }
    async uncheckActiveCheckbox() {
        await this.uncheckCheckbox(this.processActiveCheckbox);
    }
    async clickProcessCompletedCheckbox() {
        await this.checkCheckbox(this.processCompletedCheckbox);
    }
    async uncheckCompletedCheckbox() {
        await this.uncheckCheckbox(this.processCompletedCheckbox);
    }
    async clickProcessIncidentsCheckbox() {
        await this.checkCheckbox(this.processIncidentsCheckbox);
    }
    async clickRunningProcessInstancesCheckbox() {
        await this.checkCheckbox(this.processRunningInstancesCheckbox);
    }
    async clickFinishedProcessInstancesCheckbox() {
        await this.checkCheckbox(this.processFinishedInstancesCheckbox);
    }
    async toggleActiveCheckboxOn() {
        await this.uncheckCompletedCheckbox();
        await this.clickProcessIncidentsCheckbox();
        if (await this.processActiveCheckbox.isChecked({ timeout: constants_1._1_SECOND_IN_MS })) {
            await this.uncheckCheckbox(this.processActiveCheckbox);
            await (0, sleep_1.sleep)(200); // Required to skip the debounce protection on the frontend
            await this.checkCheckbox(this.processActiveCheckbox);
        }
        else {
            await this.checkCheckbox(this.processActiveCheckbox);
        }
    }
    async toggleCompletedCheckbox() {
        await this.uncheckActiveCheckbox();
        await this.clickProcessIncidentsCheckbox();
        if (await this.processCompletedCheckbox.isChecked({ timeout: constants_1._1_SECOND_IN_MS })) {
            await this.uncheckCheckbox(this.processCompletedCheckbox);
            await (0, sleep_1.sleep)(200); // Required to skip the debounce protection on the frontend
            await this.checkCheckbox(this.processCompletedCheckbox);
        }
        else {
            await this.checkCheckbox(this.processCompletedCheckbox);
        }
    }
    async selectTenant(tenant) {
        await this.clickTenantDropdown();
        await this.clickTenantDropdownOption(tenant);
    }
    async clickTenantDropdown() {
        await this.tenantDropdown.click();
    }
    async clickTenantDropdownOption(tenant = 'All tenants') {
        await this.tenantDropdownOption(tenant).click();
    }
    async checkTableForProcess(processName) {
        try {
            // Once the processTable is visible, we know we can test for the processName
            const processTable = this.page.locator('[aria-label="Process Instances Panel"]');
            await (0, test_1.expect)(processTable).toBeVisible({ timeout: constants_1._1_SECOND_IN_MS * 4 }); // This will be on the page, just takes a little time to load. This is 4 seconds simple to handle resource contention on the host machine.
            const row = this.page.locator(`tr:has-text("${processName}")`).first();
            await (0, test_1.expect)(row).toBeVisible({ timeout: constants_1._100_MS * 2 });
            const link = row.locator('a').first();
            await (0, test_1.expect)(link).toBeVisible({ timeout: constants_1._100_MS * 2 });
            await link.scrollIntoViewIfNeeded();
            await link.click({ timeout: constants_1._100_MS * 2 });
            await (0, test_1.expect)(this.page.getByText('Instance History').first()).toBeVisible({ timeout: constants_1._1_SECOND_IN_MS * 4 }); // This will be on the page, just takes a little time to load. This is 4 seconds simple to handle resource contention on the host machine.
            return true;
        }
        catch (err) {
            return false;
        }
    }
    async clickProcessInstanceLink(processName, type = 'active') {
        if (await this.checkTableForProcess(processName)) {
            return;
        }
        const MAX_ATTEMPTS = 100;
        if (type === 'active') {
            await this.toggleActiveCheckboxOn();
        }
        else {
            await this.toggleCompletedCheckbox();
        }
        for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
            try {
                if (await this.checkTableForProcess(processName)) {
                    return;
                }
                throw new Error(`Failed to open instance "${processName}" after ${MAX_ATTEMPTS} attempts.`);
            }
            catch (err) {
                if (attempt === MAX_ATTEMPTS) {
                    throw new Error(`Failed to open instance "${processName}" after ${MAX_ATTEMPTS} attempts. Last error: ${String(err)}`);
                }
                await this.page.reload();
                await (0, sleep_1.sleep)(5000);
            }
        }
    }
}
exports.OperateProcessesPage = OperateProcessesPage;
