"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const SM_8_10_1 = require("../../fixtures/SM-8.10");
const _setup_1 = require("../../test-setup.js");
const sleep_1 = require("../../utils/sleep");
const UtilitiesPage_1 = require("../../pages/SM-8.10/UtilitiesPage");
const fileUpload_1 = require("../../utils/fileUpload");
SM_8_10_1.test.describe.configure({ mode: 'parallel' });
SM_8_10_1.test.describe('Web Modeler User Flow Tests', () => {
    SM_8_10_1.test.beforeEach(async ({ navigationPage }, testInfo) => {
        await navigationPage.goToModeler((testInfo.workerIndex + 1) * 1000);
    });
    SM_8_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    (0, SM_8_10_1.test)('Form.js Integration with User Task', async ({ taskDetailsPage, taskPanelPage, modelerHomePage, navigationPage, modelerCreatePage, operateHomePage, operateProcessesPage, operateProcessInstancePage, formJsPage, page, }) => {
        SM_8_10_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'User_Task_Process_With_Form' + randomString;
        const formName = 'New form' + randomString;
        await SM_8_10_1.test.step('Open Cross Component Test Project and Create a Simple Form', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickFormOption();
            await modelerHomePage.enterFormName(formName);
            await (0, sleep_1.sleep)(10000);
            await formJsPage.deployForm();
        });
        await SM_8_10_1.test.step('Add A BPMN Template To The Project', async () => {
            await modelerHomePage.clickProjectBreadcrumb();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_10_1.test.step('Create BPMN Diagram with a User Task with an embedded Form and Start Process Instance', async () => {
            await modelerCreatePage.modelCamundaUserTaskDiagram(processName, processName, formName);
            await (0, sleep_1.sleep)(30000);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_10_1.test.step('View Process Instance in Operate and complete User Task in Tasklist', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await operateProcessInstancePage.activeIconAssertion();
            await navigationPage.goToTasklist();
            await (0, sleep_1.sleep)(30000);
            await page.reload();
            await taskPanelPage.openTask(processName);
            await (0, UtilitiesPage_1.completeTaskWithRetry)(page, taskPanelPage, taskDetailsPage, processName, 'medium');
        });
    });
    (0, SM_8_10_1.test)('Form.js Integration with Start form @tasklistV2', async ({ modelerHomePage, modelerCreatePage, navigationPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, formJsPage, }) => {
        SM_8_10_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Start_Form_Process' + randomString;
        const formName = 'New form' + randomString;
        await SM_8_10_1.test.step('Open Cross Component Test Project and Create a Simple Form', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickFormOption();
            await modelerHomePage.enterFormName(formName);
            await (0, sleep_1.sleep)(10000);
            await formJsPage.deployForm();
        });
        await SM_8_10_1.test.step('Add A BPMN Template To The Project', async () => {
            await modelerHomePage.clickProjectBreadcrumb();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_10_1.test.step('Create BPMN Diagram with a start event with an linked Form and Start Process Instance', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await (0, sleep_1.sleep)(15000);
            await modelerCreatePage.clickStartEventElement();
            await modelerCreatePage.clickGeneralPropertiesPanel();
            await modelerCreatePage.clickNameInput();
            await modelerCreatePage.fillNameInput(processName);
            await modelerCreatePage.clickIdInput();
            await modelerCreatePage.fillIdInput(processName);
            await modelerCreatePage.clickStartEventElement();
            await (0, sleep_1.sleep)(20000);
            await modelerCreatePage.hoverOnStartEvent();
            await modelerCreatePage.clickEmbedFormButton();
            await modelerCreatePage.clickForm(formName);
            await modelerCreatePage.clickEmbedButton();
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton();
            await (0, sleep_1.sleep)(20000);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_10_1.test.step('View Process Instance in Operate and check if process is complete', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            const result = await operateProcessInstancePage.assertEitherIncidentOrCompletedIconVisible();
            (0, test_1.expect)(result).toBe('completed');
        });
    });
    (0, SM_8_10_1.test)('Timer Event User Flow @tasklistV2', async ({ modelerHomePage, modelerCreatePage, operateHomePage, operateProcessInstancePage, operateProcessesPage, navigationPage, }) => {
        // Timer waits 60 s before completing; combined with goToOperate retries
        // (5 × ~3 min) this regularly exceeds the default 12-min test timeout.
        SM_8_10_1.test.slow();
        const processName = 'Timer_Event_Process' + (await (0, _setup_1.generateRandomStringAsync)(3));
        await SM_8_10_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_10_1.test.step('Create BPMN Diagram with Timer Event and Deploy Diagram', async () => {
            await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
                timeout: 120000,
            });
            await modelerCreatePage.enterDiagramName(processName);
            await (0, sleep_1.sleep)(10000);
            await modelerCreatePage.clickStartEventElement();
            await modelerCreatePage.clickGeneralPropertiesPanel();
            await modelerCreatePage.assertNameInput(processName);
            await modelerCreatePage.clickStartEventElement();
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickIntermediateBoundaryEvent();
            await modelerCreatePage.clickChangeTypeButton();
            await modelerCreatePage.clickTimerEventOption();
            await modelerCreatePage.clickTimerEventSettings();
            await modelerCreatePage.selectTimerType('timeDuration');
            await modelerCreatePage.clickTimerValue();
            await modelerCreatePage.fillTimerValue('PT60S');
            await modelerCreatePage.clickAppendElementButton();
            await modelerCreatePage.clickAppendEndEventButton();
            await (0, sleep_1.sleep)(1000);
            await modelerCreatePage.runProcessInstance();
        });
        await SM_8_10_1.test.step('Assert Diagram Has Successfully Completed in Operate', async () => {
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.processesTab).toBeVisible({ timeout: 120000 });
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await operateProcessInstancePage.activeIconAssertion();
            await operateProcessInstancePage.completedIconAssertion();
        });
    });
    (0, SM_8_10_1.test)('Deploy and run process with run configuration and verify in Operate', async ({ modelerHomePage, modelerCreatePage, navigationPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, }) => {
        SM_8_10_1.test.slow();
        const randomString = await (0, _setup_1.generateRandomStringAsync)(3);
        const processName = 'Run_Config_Process_' + randomString;
        const businessId = `bid-${Date.now()}-${randomString}`;
        const variables = '{"orderNumber": "A12BH98", "amount": 185.34}';
        await SM_8_10_1.test.step('Open Cross Component Test Project and Create a BPMN Diagram Template', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickBpmnTemplateOption();
        });
        await SM_8_10_1.test.step('Model a simple job worker diagram', async () => {
            await modelerCreatePage.modelJobWorkerDiagram(processName);
        });
        await SM_8_10_1.test.step('Deploy and run process with variables and Business ID', async () => {
            await modelerCreatePage.runProcessInstance(variables, '', businessId);
        });
        await SM_8_10_1.test.step('View process instance in Operate and assert run configuration is visible', async () => {
            await navigationPage.goToOperate();
            await (0, test_1.expect)(operateHomePage.processesTab).toBeVisible({
                timeout: 120000,
            });
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName);
            await operateProcessInstancePage.assertBusinessIdVisible(businessId);
            await operateProcessInstancePage.assertProcessVariableContainsText('orderNumber', '"A12BH98"');
            await operateProcessInstancePage.assertProcessVariableContainsText('amount', '185.34');
        });
    });
    (0, SM_8_10_1.test)('Conditional Events - Deploy, verify in Operate and verify process in Optimize dashboard @tasklistV2', async ({ page, modelerHomePage, modelerCreatePage, navigationPage, operateHomePage, operateProcessesPage, operateProcessInstancePage, optimizeHomePage, optimizeDashboardPage, }) => {
        SM_8_10_1.test.slow();
        const bpmnFileName = 'Conditional_Events_All.bpmn';
        const processName = 'Conditional Events Auto-Run';
        await SM_8_10_1.test.step('Open Cross Component Test Project and upload BPMN file', async () => {
            await modelerHomePage.clickCrossComponentProjectFolder();
            await modelerHomePage.clickDiagramTypeDropdown();
            await modelerHomePage.clickUploadFilesButton();
            await (0, fileUpload_1.uploadFile)(page, bpmnFileName);
        });
        await SM_8_10_1.test.step('Open uploaded BPMN diagram', async () => {
            await (0, sleep_1.sleep)(5000);
            await modelerHomePage.clickProcessDiagram(bpmnFileName.replace('.bpmn', ''));
        });
        await SM_8_10_1.test.step('Deploy and run process with variable', async () => {
            const variables = '{"myVar": 8}';
            await modelerCreatePage.runProcessInstance(variables);
        });
        await SM_8_10_1.test.step('View process instance in Operate and verify completion', async () => {
            await navigationPage.goToOperate();
            await operateHomePage.clickProcessesTab();
            await operateProcessesPage.clickProcessInstanceLink(processName, 'completed');
            await operateProcessInstancePage.assertProcessCompleteStatusWithRetry(60000, 15);
            await operateProcessInstancePage.assertProcessVariableContainsText('myVar', '8');
        });
        if (process.env.IS_OPTIMIZE !== 'false') {
            await SM_8_10_1.test.step('Navigate to Optimize and verify process appears in dashboard (CE-OPT-01)', async () => {
                await navigationPage.goToOptimize();
                await optimizeHomePage.clickDashboardLink();
                await (0, sleep_1.sleep)(60000);
                await page.reload();
                await optimizeDashboardPage.clickFilterTable();
                await optimizeDashboardPage.fillFilterTable(processName);
                await optimizeDashboardPage.processAssertion(processName);
            });
            await SM_8_10_1.test.step('Verify the Conditional Events process link is navigable in the Optimize dashboard (CE-OPT-02)', async () => {
                await page.reload();
                await optimizeDashboardPage.clickFilterTable();
                await optimizeDashboardPage.fillFilterTable(processName);
                await optimizeDashboardPage.processAssertion(processName);
                await page.getByTitle(processName).first().click({ timeout: 60000 });
                await (0, test_1.expect)(optimizeHomePage.optimizeBanner).toBeVisible({
                    timeout: 60000,
                });
            });
        }
    });
});
