"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createKeycloakUser = exports.createUser = exports.setupKeycloakUser = exports.keycloakAdminCredentials = void 0;
const test_1 = require("@playwright/test");
const keycloakAdminCredentials = () => {
    return {
        username: 'admin',
        password: process.env.DISTRO_QA_E2E_TESTS_KEYCLOAK_PASSWORD,
    };
};
exports.keycloakAdminCredentials = keycloakAdminCredentials;
const setupKeycloakUser = async (navigationPage, identityPage, keycloakLoginPage, keycloakAdminPage, ocIdentityHomePage, ocIdentityRolesPage, assignToDefaultTenant = false, managementIdentityTenantPage) => {
    const testUsername = `test-user-${new Date().getTime()}-${Math.random()
        .toString(36)
        .substring(7)}`;
    const testPassword = 'test-password';
    await createKeycloakUser(navigationPage, keycloakLoginPage, keycloakAdminPage, testUsername, testPassword);
    await createIdentityUser(navigationPage, identityPage, testUsername);
    if (assignToDefaultTenant && managementIdentityTenantPage) {
        const userEmail = `qacamunda+${testUsername}@gmail.com`;
        await navigationPage.goToManagementIdentity();
        await (0, test_1.expect)(identityPage.usersLink).toBeVisible({ timeout: 10000 });
        if (await identityPage.tenantTab.isVisible()) {
            await managementIdentityTenantPage.assignUserToTenant('Default', testUsername, userEmail);
        }
    }
    await createOCUser(navigationPage, ocIdentityHomePage, ocIdentityRolesPage, testUsername);
    return { testUsername, testPassword };
};
exports.setupKeycloakUser = setupKeycloakUser;
async function createUser(navigationPage, identityPage, keycloakLoginPage, keycloakAdminPage, username, password) {
    await createKeycloakUser(navigationPage, keycloakLoginPage, keycloakAdminPage, username, password);
    await createIdentityUser(navigationPage, identityPage, username);
}
exports.createUser = createUser;
async function createKeycloakUser(navigationPage, keycloakLoginPage, keycloakAdminPage, username, password) {
    await navigationPage.goToKeycloak();
    await keycloakLoginPage.toBeVisible();
    await keycloakLoginPage.fillUsername((0, exports.keycloakAdminCredentials)().username);
    await keycloakLoginPage.fillPassword((0, exports.keycloakAdminCredentials)().password);
    await keycloakLoginPage.clickLogin();
    await keycloakAdminPage.toBeVisible();
    await keycloakAdminPage.switchToCamundaPlatform();
    await keycloakAdminPage.clickUsersTab();
    await keycloakAdminPage.clickAddUser();
    await keycloakAdminPage.fillUser(username);
    await keycloakAdminPage.saveUser();
    await keycloakAdminPage.fillPassword(password);
    await keycloakAdminPage.savePassword();
}
exports.createKeycloakUser = createKeycloakUser;
const createIdentityUser = async (navigationPage, managementIdentity, username) => {
    await navigationPage.goToManagementIdentity();
    await managementIdentity.clickUsersLink();
    await managementIdentity.clickUser(username);
    await managementIdentity.clickAssignedRolesTab();
    await managementIdentity.clickAssignRolesButton();
    if (process.env.IS_OPTIMIZE !== 'false') {
        await managementIdentity.clickOptimizeCheckbox();
    }
    await managementIdentity.clickModelerCheckbox();
    await managementIdentity.clickConsoleCheckbox();
    await managementIdentity.clickIdentityCheckbox();
    await managementIdentity.clickOrchestrationCheckbox();
    await managementIdentity.clickAddButton();
};
const createOCUser = async (navigationPage, ocIdentityHomePage, ocIdentityRolesPage, username) => {
    if (process.env.IS_MT === 'true') {
        await navigationPage.goToOCAdmin();
    }
    else {
        await navigationPage.goToOCIdentity();
    }
    await ocIdentityHomePage.clickRolesTab();
    await ocIdentityRolesPage.clickAdminRole();
    await ocIdentityRolesPage.assignUserToRole(username);
};
