"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlayPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const maxWaitTimeSeconds = 120000;
class PlayPage {
    page;
    completeJobButton;
    restartProcess;
    diagram;
    startInstanceButton;
    loadingInstanceDetailsText;
    dialog;
    constructor(page) {
        this.page = page;
        this.completeJobButton = page
            .getByTestId('diagram')
            .getByLabel('Complete job');
        this.restartProcess = this.page.getByRole('button', {
            name: 'Restart process',
        });
        this.diagram = page.getByTestId('diagram');
        this.startInstanceButton = this.page
            .getByTestId('diagram')
            .getByLabel('Start instance');
        this.loadingInstanceDetailsText = this.page.getByText('Loading instance details...');
        this.dialog = page.getByRole('dialog');
    }
    async waitForCompleteJobButtonToBeAvailable() {
        await (0, test_1.expect)(this.completeJobButton).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
    async clickCompleteJobButton() {
        await (0, test_1.expect)(this.completeJobButton).toBeVisible({ timeout: 60000 });
        await (0, test_1.expect)(this.completeJobButton).toBeEnabled({ timeout: 60000 });
        await this.completeJobButton.click();
    }
    async clickStartInstanceButton() {
        const maxRetries = 3;
        let attempts = 0;
        while (attempts < maxRetries) {
            try {
                await this.diagram.waitFor({ state: 'visible', timeout: 30000 });
                await this.startInstanceButton.click({ timeout: 30000 });
                await (0, test_1.expect)(this.loadingInstanceDetailsText).toBeVisible();
                await (0, test_1.expect)(this.loadingInstanceDetailsText).not.toBeVisible({
                    timeout: 60000,
                });
                await (0, test_1.expect)(this.dialog).not.toBeVisible({ timeout: 15000 });
                return;
            }
            catch (error) {
                if (attempts >= maxRetries - 1)
                    throw error;
                await this.page.reload();
                await (0, sleep_1.sleep)(2000);
                attempts++;
            }
        }
    }
    async dismissStartModal() {
        await this.page
            .getByRole('button', {
            name: 'Start a process instance',
        })
            .click();
    }
    async waitForInstanceDetailsToBeLoaded() {
        const maxRetries = 3;
        let attempts = 0;
        while (attempts < maxRetries) {
            try {
                await (0, test_1.expect)(this.page.getByText(/process instance key/i)).toBeVisible({
                    timeout: maxWaitTimeSeconds,
                });
                await (0, test_1.expect)(this.page.getByText(/This process instance has no variables/i)).toBeVisible({
                    timeout: maxWaitTimeSeconds,
                });
                return;
            }
            catch (error) {
                if (attempts >= maxRetries - 1)
                    throw error;
                await this.page.reload();
                const startButton = this.restartProcess.or(this.startInstanceButton);
                await startButton.waitFor({ state: 'visible', timeout: 30000 });
                await startButton.click();
                attempts++;
            }
        }
    }
    async waitForNextElementToBeActive(historyItem) {
        await (0, test_1.expect)(this.page.getByText(new RegExp(`^${historyItem}`, 'i'))).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
    async waitForProcessToBeCompleted() {
        await (0, test_1.expect)(this.page.getByRole('heading', { name: 'Congratulations, you did it!' })).toBeVisible({ timeout: maxWaitTimeSeconds });
    }
}
exports.PlayPage = PlayPage;
