"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperateProcessInstancePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class OperateProcessInstancePage {
    page;
    diagram;
    completedIcon;
    diagramSpinner;
    activeIcon;
    incidentIcon;
    userTaskOpenTasklistLink;
    taskDetailsPopOver;
    connectorResultVariableName;
    variablesList;
    messageVariable;
    whatsNewPopUp;
    gotItButton;
    constructor(page) {
        this.page = page;
        this.diagram = page.getByTestId('diagram');
        this.completedIcon = page
            .getByTestId('instance-header')
            .getByTestId('COMPLETED-icon');
        this.diagramSpinner = page.getByTestId('diagram-spinner');
        this.activeIcon = page
            .getByTestId('instance-header')
            .getByTestId('ACTIVE-icon');
        this.incidentIcon = page
            .getByTestId('instance-header')
            .getByTestId('INCIDENT-icon');
        this.userTaskOpenTasklistLink = page.getByRole('link', {
            name: 'Open Tasklist',
        });
        this.taskDetailsPopOver = page.getByTestId('popover');
        this.connectorResultVariableName = (name) => page.getByTestId('variable-' + name);
        this.variablesList = page.getByTestId('variables-list');
        this.messageVariable = page.getByTestId('variable-message');
        this.whatsNewPopUp = page.getByRole('heading', {
            name: "Here's what moved in Operate",
        });
        this.gotItButton = page.getByRole('button', { name: 'Got it' });
    }
    async connectorResultVariableValue(variableName) {
        return await this.page.getByTestId(variableName).locator('td').last();
    }
    async closePopOverIfVisible() {
        await (0, test_1.expect)(this.page.getByText('Instance History').first()).toBeVisible({
            timeout: 60000,
        });
        if (await this.whatsNewPopUp.isVisible()) {
            await this.gotItButton.click();
        }
    }
    async reload() {
        await this.page.reload();
    }
    async assertProcessCompleteStatusWithRetry(timeout = 60000, maxRetries = 10) {
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            try {
                await (0, test_1.expect)(this.completedIcon).toBeVisible({
                    timeout: timeout,
                });
                return;
            }
            catch (error) {
                if (attempt < maxRetries - 1) {
                    console.warn(`Process complete status attempt ${attempt + 1} failed. Retrying...`);
                    await this.page.reload();
                    await (0, sleep_1.sleep)(5000);
                }
                else {
                    throw new Error(`Assertion failed after ${maxRetries} attempts`);
                }
            }
        }
    }
    async assertProcessVariableContainsText(variableName, text) {
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(`variable-${variableName}`)).toContainText(text, { timeout: 30000 });
                return;
            }
            catch (error) {
                console.log(`Failed to assert variable ${variableName}` + error);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        throw new Error(`Failed to assert variable ${variableName} after ${maxRetries} attempts.`);
    }
    // Operate's variable cell renders document variables as "N documents"
    // (or "1 document" for singular) — filenames and metadata are no longer
    // shown inline, and clicking the "Open" control opens an empty detail
    // modal. Per-field assertions can't be verified through the UI; assert
    // just the count so the test still verifies the variable was populated
    // with the expected number of documents.
    //
    // `softFail`: when true, log a warning and return instead of throwing if
    // the variable never appears. Use this for variables that a later flow
    // step is expected to delete (e.g. "Delete doc in storage" removes
    // `doc` from process state before the instance completes).
    async assertProcessVariableHasDocumentCount(variableName, count, softFail = false) {
        const expected = count === 1 ? '1 document' : `${count} documents`;
        const maxRetries = 3;
        for (let retries = 0; retries < maxRetries; retries++) {
            try {
                await (0, test_1.expect)(this.page.getByTestId(`variable-${variableName}`)).toContainText(expected, { timeout: 30000 });
                return;
            }
            catch (error) {
                console.log(`Failed to assert document count for ${variableName}` + error);
                await this.page.reload();
                await (0, sleep_1.sleep)(10000);
            }
        }
        const msg = `Failed to assert document count for ${variableName} after ${maxRetries} attempts.`;
        if (softFail) {
            console.warn(`${msg} (softFail=true — continuing; variable may have been deleted by a later flow step.)`);
            return;
        }
        throw new Error(msg);
    }
    async clickDiagramTask(taskName) {
        const diagramTask = this.diagram.getByText(taskName);
        await (0, test_1.expect)(diagramTask).toBeVisible();
        await diagramTask.click();
    }
    async assertActiveTokenIsPresent() {
        await (0, test_1.expect)(this.activeIcon).toBeVisible({ timeout: 60000 });
    }
    async assertDetailsPopoverIsVisible() {
        await (0, test_1.expect)(this.taskDetailsPopOver).toBeVisible({
            timeout: 60000,
        });
    }
    async assertBusinessIdVisible(businessId) {
        const instanceHeader = this.page.getByTestId('instance-header');
        await (0, test_1.expect)(instanceHeader.getByText('Business ID')).toBeVisible({
            timeout: 60000,
        });
        await (0, test_1.expect)(instanceHeader.getByText(businessId, { exact: true })).toBeVisible({ timeout: 60000 });
    }
}
exports.OperateProcessInstancePage = OperateProcessInstancePage;
