"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlayPage = void 0;
const test_1 = require("@playwright/test");
const clickLocatorWithRetry_1 = require("../../utils/assertionHelpers/clickLocatorWithRetry");
const maxWaitTimeSeconds = 120000;
class PlayPage {
    page;
    completeJobButton;
    configureTestPanel;
    configureTestPanelStartButton;
    startInstanceOverlayButton;
    constructor(page) {
        this.page = page;
        this.completeJobButton = page
            .getByTestId('diagram')
            .getByLabel('Complete job');
        // Newer Modeler Play UI: the "Configure Scenario" floating card hosts a
        // primary "Start" Carbon button (variants: "Start", "Start with
        // variables", "Start with a form"). Source:
        // camunda-hub frontend/packages/modeler/play/src/Definition/
        //   ConfigureTestPanel/index.tsx — renders <FloatingCard
        //   data-testid="configure-test-panel"> with a <Button> whose visible
        //   text starts with "Start".
        this.configureTestPanel = page.getByTestId('configure-test-panel');
        this.configureTestPanelStartButton = this.configureTestPanel.getByRole('button', { name: /^Start( with .*)?$/, exact: false });
        // Legacy bpmn-js canvas overlay button (still emitted by older Modeler
        // builds, also relabeled with cached/example-data suffixes). Kept as a
        // fallback for environments that haven't shipped the ConfigureTestPanel
        // yet.
        this.startInstanceOverlayButton = page
            .getByTestId('diagram')
            .getByLabel('Start instance', { exact: false });
    }
    async waitForCompleteJobButtonToBeAvailable() {
        await (0, test_1.expect)(this.completeJobButton).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
    async clickCompleteJobButton() {
        await this.completeJobButton.click();
    }
    async clickStartInstanceButton() {
        const startTrigger = this.configureTestPanelStartButton
            .or(this.startInstanceOverlayButton)
            .first();
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, startTrigger, {
            totalTimeout: 240000,
            visibilityTimeout: 60000,
            maxRetries: 8,
            retryDelayMs: 5000,
        });
    }
    async dismissStartModal() {
        // The intro dialog cycles labels across Modeler versions/states.
        const buttonVariations = [
            'Start a process instance',
            'Start another instance',
            'Start new instance',
            'Start instance',
        ];
        for (const buttonName of buttonVariations) {
            const button = this.page.getByRole('button', { name: buttonName });
            if ((await button.count()) > 0) {
                await button.first().click();
                return;
            }
        }
    }
    async waitForInstanceDetailsToBeLoaded() {
        const maxRetries = 2;
        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();
                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.getByText('Completed')).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
}
exports.PlayPage = PlayPage;
