"use strict";
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.completeTaskWithRetry = exports.validateURL = exports.getAccessTokenFor = exports.navigateToApp = exports.getBaseAppUrl = exports.dockerBaseUrls = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
exports.dockerBaseUrls = {
    operate: 'http://localhost:8081',
    tasklist: 'http://localhost:8082',
    identity: 'http://localhost:8084',
};
function getBaseAppUrl(appName) {
    if (process.env.RUN_AS_DOCKER_COMPOSE != 'true') {
        return 'http://localhost:8080';
    }
    return exports.dockerBaseUrls[appName];
}
exports.getBaseAppUrl = getBaseAppUrl;
async function navigateToApp(page, appName) {
    if (process.env.RUN_AS_DOCKER_COMPOSE === 'true') {
        if (appName == 'operate') {
            await page.goto(exports.dockerBaseUrls[appName] + '/operate');
        }
        else if (appName == 'tasklist') {
            await page.goto(exports.dockerBaseUrls[appName] + '/tasklist');
        }
        else if (appName == 'identity') {
            await page.goto(exports.dockerBaseUrls[appName]);
        }
    }
    else {
        await page.goto('/' + appName.toLowerCase() + '/login');
    }
}
exports.navigateToApp = navigateToApp;
async function getAccessTokenFor(request, appName) {
    const response = await request.post('http://localhost:18080/auth/realms/camunda-platform/protocol/openid-connect/token', {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        form: {
            client_id: appName,
            client_secret: process.env.APP_CLIENT_SECRET,
            grant_type: 'client_credentials',
        },
    });
    const body = await response.json();
    return body.access_token;
}
exports.getAccessTokenFor = getAccessTokenFor;
async function validateURL(page, URL) {
    (0, test_1.expect)(page).toHaveURL(URL);
}
exports.validateURL = validateURL;
async function completeTaskWithRetry(taskPanelPage, taskDetailsPage, taskName, taskPriority, maxRetries = 5) {
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            await taskPanelPage.openTask(taskName);
            await (0, sleep_1.sleep)(500);
            if (!(await taskDetailsPage.assignedToMeText.isVisible())) {
                await taskDetailsPage.clickAssignToMeButton();
            }
            await (0, test_1.expect)(taskDetailsPage.detailsPanel.getByText(taskPriority)).toBeVisible();
            await taskDetailsPage.taskCompletedBanner.waitFor({ state: 'hidden' });
            await taskDetailsPage.clickCompleteTaskButton();
            await (0, test_1.expect)(taskPanelPage.availableTasks.getByText(taskName, { exact: true }).first()).not.toBeVisible();
            return;
        }
        catch (error) {
            if (attempt < maxRetries - 1) {
                console.warn(`Attempt ${attempt + 1} failed for completing task ${taskName}. Retrying...`);
            }
            else {
                throw new Error(`Assertion failed after ${maxRetries} attempts` + error);
            }
        }
    }
}
exports.completeTaskWithRetry = completeTaskWithRetry;
