"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 expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
const clickLocatorWithRetry_1 = require("../../utils/assertionHelpers/clickLocatorWithRetry");
const maxWaitTimeSeconds = 180000;
class PlayPage {
    page;
    completeJobButton;
    saveScenarioButton;
    viewScenarioButton;
    runAllScenariosButton;
    confirmSaveScenarioButton;
    enterScenarioNameInput;
    confirmDeleteScenarioButton;
    viewAllScenariosButton;
    getScenarioRowByName;
    getScenarioRow;
    diagram;
    startInstanceButton;
    startInstanceWithCachedButton;
    configureTestPanel;
    configureTestPanelStartButton;
    notifications;
    saveScenarioModal;
    loadingInstanceDetailsText;
    retryButton;
    dialog;
    inputPanelStartButton;
    constructor(page) {
        this.page = page;
        this.dialog = page.getByRole('dialog');
        this.completeJobButton = page
            .getByTestId('diagram')
            .getByLabel('Complete job');
        this.saveScenarioButton = page.getByRole('button', { name: 'Save scenario' });
        this.viewScenarioButton = page.getByRole('button', { name: 'View scenario' });
        this.runAllScenariosButton = page.getByRole('button', {
            name: 'Run all scenarios',
        });
        this.confirmSaveScenarioButton = page
            .getByTestId('save-scenario-modal')
            .getByRole('button', { name: 'Save' });
        this.enterScenarioNameInput = page
            .getByTestId('save-scenario-modal')
            .locator('input#scenario-name')
            .first();
        this.confirmDeleteScenarioButton = page
            .getByRole('dialog', { name: 'Delete scenario' })
            .getByRole('button', { name: 'Delete', exact: true });
        this.viewAllScenariosButton = page.getByText('(View all)').last();
        this.getScenarioRowByName = (scenarioName) => page.locator('tr', { hasText: scenarioName });
        this.getScenarioRow = (scenarioName) => page.locator('tr', { hasText: scenarioName });
        this.diagram = page.getByTestId('diagram');
        this.startInstanceButton = this.diagram.getByLabel('Start instance', {
            exact: true,
        });
        this.startInstanceWithCachedButton = page.getByLabel('Start instance with cached');
        this.configureTestPanel = page.getByTestId('configure-test-panel');
        this.configureTestPanelStartButton = this.configureTestPanel.getByRole('button', { name: /^Start( with .*)?$/, exact: false });
        this.notifications = page.locator('.cds--toast-notification');
        this.saveScenarioModal = page.getByTestId('save-scenario-modal');
        this.loadingInstanceDetailsText = this.page.getByText('Loading instance details...');
        this.retryButton = this.page.getByRole('button', { name: 'Retry' });
        // New Play UI (8.10+): "Configure scenario" opens an input panel with a plain "Start" button.
        this.inputPanelStartButton = page.getByRole('button', {
            name: 'Start',
            exact: true,
        });
    }
    async waitForCompleteJobButtonToBeAvailable() {
        await (0, test_1.expect)(this.completeJobButton).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
    async clickCompleteJobButton() {
        await this.completeJobButton.waitFor({ state: 'visible', timeout: 30000 });
        await this.completeJobButton.click();
    }
    async clickStartInstanceButton() {
        const startTrigger = this.configureTestPanelStartButton
            .or(this.inputPanelStartButton)
            .or(this.startInstanceButton)
            .or(this.startInstanceWithCachedButton)
            .first();
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, startTrigger, {
            totalTimeout: maxWaitTimeSeconds,
            visibilityTimeout: 30000,
            maxRetries: 5,
        });
    }
    async dismissStartModal() {
        const buttonVariations = [
            'Start a process instance',
            'Start another instance',
            'Start new instance',
            'Start instance',
        ];
        // Wait for the page to settle before checking for modal buttons
        await this.page.waitForLoadState('domcontentloaded');
        const maxAttempts = 3;
        for (let attempt = 0; attempt < maxAttempts; attempt++) {
            for (const buttonName of buttonVariations) {
                const button = this.page.getByRole('button', { name: buttonName });
                const count = await button.count();
                if (count > 0) {
                    await button.waitFor({ state: 'visible', timeout: 10000 });
                    await button.click();
                    return;
                }
            }
            if (attempt < maxAttempts - 1) {
                await this.page.waitForLoadState('load');
            }
        }
        // No modal present — new Play UI skips the modal entirely (Configure scenario
        // opens the input panel directly without a modal step).
    }
    async clickStartInstanceWithCachedButton() {
        await this.diagram.waitFor({ state: 'visible', timeout: 30000 });
        await this.startInstanceWithCachedButton.click({ timeout: 30000 });
    }
    async waitForInstanceDetailsToBeLoaded() {
        // Do NOT use page.reload() — it destroys Play's embedded context.
        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,
        });
    }
    async waitForNextElementToBeActive(historyItem) {
        const locator = this.page.getByText(new RegExp(`^${historyItem}`, 'i'));
        // Do NOT use page.reload() here — Play is embedded in the modeler and
        // reloading destroys the Play instance context, making the diagram
        // permanently invisible. Use a long timeout instead, consistent with
        // waitForCompleteJobButtonToBeAvailable.
        await (0, test_1.expect)(locator.first()).toBeVisible({ timeout: maxWaitTimeSeconds });
    }
    async waitForProcessToBeCompleted() {
        // Do NOT use page.reload() — it destroys Play's embedded context.
        await (0, test_1.expect)(this.page.getByText('Completed')).toBeVisible({
            timeout: maxWaitTimeSeconds,
        });
    }
    async clickSaveScenarioButton() {
        await this.notifications
            .first()
            .waitFor({ state: 'hidden', timeout: 60000 })
            .catch(() => { });
        const isModalVisible = await this.saveScenarioModal
            .isVisible()
            .catch(() => false);
        if (!isModalVisible) {
            await (0, sleep_1.sleep)(3000);
            await (0, test_1.expect)(this.saveScenarioButton).toBeVisible({ timeout: 30000 });
            await (0, test_1.expect)(this.saveScenarioButton).toBeEnabled({ timeout: 30000 });
            await this.saveScenarioButton.click({ force: true, timeout: 30000 });
        }
    }
    async clickViewScenarioButton() {
        await this.viewScenarioButton.waitFor({ state: 'visible', timeout: 30000 });
        await this.viewScenarioButton.click();
    }
    async enterScenarioName(scenarioName) {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.dialog, {
            postAction: async () => {
                await this.saveScenarioButton.click({ timeout: 30000 });
            },
        });
        const input = this.dialog.locator('input#scenario-name');
        await input.waitFor({ state: 'visible', timeout: 10000 });
        await input.clear();
        await input.fill(scenarioName);
    }
    async updateScenarioName(newScenarioName) {
        await this.enterScenarioNameInput.fill(newScenarioName);
    }
    async clickViewAllScenariosButton() {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.viewAllScenariosButton);
        await this.viewAllScenariosButton.click();
    }
    async confirmSaveScenario() {
        await this.confirmSaveScenarioButton.waitFor({
            state: 'visible',
            timeout: 10000,
        });
        await this.confirmSaveScenarioButton.click();
    }
    async getDeleteIconForScenario(scenarioName) {
        const scenarioRow = this.page
            .locator('tr')
            .filter({ has: this.page.getByText(scenarioName, { exact: true }) });
        return scenarioRow.getByLabel('Delete scenario');
    }
    async deleteScenario(scenarioName) {
        const deleteIcon = await this.getDeleteIconForScenario(scenarioName);
        await deleteIcon.waitFor({ state: 'visible', timeout: 10000 });
        await deleteIcon.click();
    }
    async confirmDeleteScenario() {
        await this.confirmDeleteScenarioButton.waitFor({
            state: 'visible',
            timeout: 10000,
        });
        await this.confirmDeleteScenarioButton.click();
    }
    async clickRunAllScenariosButton() {
        await this.runAllScenariosButton.waitFor({
            state: 'visible',
            timeout: 30000,
        });
        await this.runAllScenariosButton.click();
    }
    async clickmessagePublishButton() {
        await this.diagram.waitFor({ state: 'visible', timeout: 30000 });
        const publishIcon = this.diagram
            .locator('div.djs-overlays')
            .getByLabel('Publish message', { exact: true });
        await publishIcon.click({ timeout: 30000 });
    }
    async assertScenarioCompleted(scenarioName) {
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.page
            .locator('tr', { hasText: scenarioName })
            .locator('text=Completed'), {
            totalTimeout: 120000,
            visibilityTimeout: 30000,
            maxRetries: 5,
            postAction: async () => {
                if (await this.retryButton.isVisible()) {
                    await this.retryButton.click();
                }
            },
        });
    }
}
exports.PlayPage = PlayPage;
