"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.modelAndRunConnectorsTimerEventDiagram = exports.assertPageTextWithRetry = exports.assertLocatorVisibleWithRetry = exports.modelRestConnector = exports.completeTaskWithRetry = exports.createAndRunProcess = exports.runMultipleProcesses = exports.deleteAllUserGroups = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const fileUpload_1 = require("../../utils/fileUpload");
async function deleteAllUserGroups(navigationPage, identityPage) {
    await navigationPage.goToIdentity();
    await identityPage.clickGroupsTab();
    await identityPage.deleteAllGroups();
}
exports.deleteAllUserGroups = deleteAllUserGroups;
async function runMultipleProcesses(page, modelerHomePage, modelerCreatePage, numberOfProcesses, processName, processId = '') {
    for (let i = 1; i <= numberOfProcesses; i++) {
        await createAndRunProcess(page, modelerHomePage, modelerCreatePage, processName + i, processId.length > 0 ? processId + i : processId);
    }
}
exports.runMultipleProcesses = runMultipleProcesses;
async function createAndRunProcess(page, modelerHomePage, modelerCreatePage, processName, processId = '') {
    await modelerHomePage.clickDiagramTypeDropdown();
    await modelerHomePage.clickBpmnTemplateOption();
    await modelerCreatePage.modelCamundaUserTaskDiagram(processName, processId);
    await modelerCreatePage.runProcessInstance();
    await modelerHomePage.clickProjectBreadcrumb();
}
exports.createAndRunProcess = createAndRunProcess;
async function completeTaskWithRetry(page, taskPanelPage, taskDetailsPage, taskName, taskPriority, maxRetries = 3) {
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            await taskPanelPage.openTask(taskName);
            await (0, sleep_1.sleep)(2000);
            if (!(await taskDetailsPage.assignedToMeText.isVisible())) {
                await taskDetailsPage.clickAssignToMeButton();
            }
            await (0, test_1.expect)(taskDetailsPage.detailsPanel.getByText(taskPriority)).toBeVisible();
            await taskDetailsPage.clickCompleteTaskButton();
            await (0, test_1.expect)(page.getByText('Task completed').first()).toBeVisible({
                timeout: 200000,
            });
            await (0, test_1.expect)(taskPanelPage.availableTasks.getByText(taskName, { exact: true }).first()).not.toBeVisible({ timeout: 10000 });
            return;
        }
        catch (error) {
            if (attempt < maxRetries - 1) {
                await page.reload();
                console.warn(`Attempt ${attempt + 1} failed for completing task ${taskName}. Retrying...`);
            }
            else {
                console.error(error);
                throw new Error(`Assertion failed after ${maxRetries} attempts`);
            }
        }
    }
}
exports.completeTaskWithRetry = completeTaskWithRetry;
async function modelRestConnector(modelerCreatePage, connectorSettingsPage, connectorMarketplacePage, processName, url, auth, resultExpression, resultVariable = '', basicAuthCredentials = {
    username: '',
    password: '',
}) {
    await (0, test_1.expect)(modelerCreatePage.generalPanel).toBeVisible({
        timeout: 120000,
    });
    await modelerCreatePage.enterDiagramName(processName);
    await (0, sleep_1.sleep)(10000);
    await modelerCreatePage.clickAppendElementButton();
    await modelerCreatePage.clickAppendTaskButton();
    await modelerCreatePage.clickChangeTypeButton();
    try {
        await (0, test_1.expect)(modelerCreatePage.restConnectorOption).toBeVisible({
            timeout: 15000,
        });
    }
    catch (error) {
        await modelerCreatePage.clickMarketPlaceButton();
        await connectorMarketplacePage.clickSearchForConnectorTextbox();
        await connectorMarketplacePage.fillSearchForConnectorTextbox('REST Connector');
        await (0, sleep_1.sleep)(10000);
        await connectorMarketplacePage.downloadConnectorToProject();
        await modelerCreatePage.clickChangeTypeButton();
    }
    await modelerCreatePage.clickRestConnectorOption();
    if (auth == 'bearer') {
        await connectorSettingsPage.selectAuthenticationType('bearer');
        await (0, test_1.expect)(connectorSettingsPage.bearerTokenInput).toBeVisible({
            timeout: 60000,
        });
        await connectorSettingsPage.clickBearerTokenInput();
        await connectorSettingsPage.fillBearerTokenInput('thisisabearertoken');
        await (0, test_1.expect)(connectorSettingsPage.methodTypeDropdown).toBeVisible({
            timeout: 30000,
        });
        await connectorSettingsPage.selectMethodType('POST');
    }
    else if (auth == 'basic') {
        await connectorSettingsPage.selectAuthenticationType('basic');
        await (0, test_1.expect)(connectorSettingsPage.usernameInput).toBeVisible({
            timeout: 60000,
        });
        await connectorSettingsPage.clickUsernameInput();
        (0, test_1.expect)(basicAuthCredentials['username'].length).toBeGreaterThan(0);
        (0, test_1.expect)(basicAuthCredentials['password'].length).toBeGreaterThan(0);
        await connectorSettingsPage.fillUsernameInput(basicAuthCredentials['username']);
        await connectorSettingsPage.clickPasswordInput();
        await connectorSettingsPage.fillPasswordInput(basicAuthCredentials['password']);
        await connectorSettingsPage.selectMethodType('POST');
    }
    await connectorSettingsPage.clickUrlInput();
    await connectorSettingsPage.fillUrlInput(url);
    if (resultVariable.length > 0) {
        await (0, test_1.expect)(connectorSettingsPage.resultVariableInput).toBeVisible({
            timeout: 60000,
        });
        await connectorSettingsPage.clickResultVariableInput();
        await connectorSettingsPage.fillResultVariableInput('result');
    }
    await connectorSettingsPage.clickResultExpressionInput();
    await connectorSettingsPage.fillResultExpressionInput(resultExpression);
    await modelerCreatePage.clickAppendElementButton();
    await modelerCreatePage.clickAppendEndEventButton();
    await (0, sleep_1.sleep)(20000);
}
exports.modelRestConnector = modelRestConnector;
async function assertLocatorVisibleWithRetry(page, locator, text, timeout = 120000, maxRetries = 3) {
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            await page.reload();
            await (0, test_1.expect)(locator).toBeVisible({
                timeout: timeout,
            });
            return;
        }
        catch (error) {
            if (attempt < maxRetries - 1) {
                console.warn(`Attempt ${attempt + 1} failed for asserting ${text}. Retrying...`);
            }
            else {
                throw new Error(`Assertion failed after ${maxRetries} attempts`);
            }
        }
    }
}
exports.assertLocatorVisibleWithRetry = assertLocatorVisibleWithRetry;
async function assertPageTextWithRetry(page, text, notVisible, timeout = 120000, maxRetries = 3) {
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            await page.reload();
            if (notVisible == true) {
                await (0, sleep_1.sleep)(10000);
                await (0, test_1.expect)(page.getByText(text, { exact: true }).first()).not.toBeVisible({
                    timeout: timeout,
                });
            }
            else {
                await (0, test_1.expect)(page.getByText(text, { exact: true }).first()).toBeVisible({
                    timeout: timeout,
                });
            }
            return;
        }
        catch (error) {
            if (attempt < maxRetries - 1) {
                console.warn(`Attempt ${attempt + 1} failed for asserting ${text}. Retrying...`);
            }
            else {
                throw new Error(`Assertion failed after ${maxRetries} attempts`);
            }
        }
    }
}
exports.assertPageTextWithRetry = assertPageTextWithRetry;
async function modelAndRunConnectorsTimerEventDiagram(modelerCreatePage, modelerHomePage, processName, page, connectorMarketplacePage, connectorTemplatePage) {
    await modelerHomePage.clickCrossComponentProjectFolder();
    await modelerHomePage.clickDiagramTypeDropdown();
    await modelerHomePage.clickUploadFilesButton();
    await (0, fileUpload_1.uploadFile)(page, 'Rest Connector Timer Event.bpmn');
    await modelerHomePage.clickProcessDiagram('Rest Connector Timer Event');
    await modelerCreatePage.clickDiagramBreadcrumb();
    await modelerCreatePage.clickEditDiagramNameButton();
    await modelerCreatePage.enterDiagramName(processName);
    await modelerCreatePage.clickStartEventElement();
    await modelerCreatePage.clickGeneralPropertiesPanel();
    await modelerCreatePage.clickIdInput();
    await modelerCreatePage.fillIdInput(processName);
    await modelerCreatePage.clickEighthPlacedElement();
    await modelerCreatePage.clickChangeTypeButton();
    await (0, test_1.expect)(modelerCreatePage.marketPlaceButton).toBeVisible({
        timeout: 120000,
    });
    await modelerCreatePage.clickMarketPlaceButton();
    await connectorMarketplacePage.clickSearchForConnectorTextbox();
    await connectorMarketplacePage.fillSearchForConnectorTextbox('REST Connector');
    await (0, sleep_1.sleep)(10000);
    await (0, test_1.expect)(page.getByText('REST ConnectorBy CamundaConnect, interact, and synchronize processes')).toBeVisible({
        timeout: 60000,
    });
    await connectorMarketplacePage.downloadConnectorToProject();
    await modelerHomePage.clickProjectBreadcrumb();
    await modelerHomePage.clickProcessDiagram('REST Outbound Connector');
    await connectorTemplatePage.publishConnector();
    await modelerHomePage.clickProjectBreadcrumb();
    await modelerHomePage.clickProcessDiagram(processName);
    await (0, sleep_1.sleep)(5000);
    await page.reload();
    await (0, sleep_1.sleep)(5000);
    await modelerCreatePage.runProcessInstance();
}
exports.modelAndRunConnectorsTimerEventDiagram = modelAndRunConnectorsTimerEventDiagram;
