"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelerUserInvitePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class ModelerUserInvitePage {
    page;
    buttonAddUser;
    inputFieldUserEmailAddress;
    buttonSendInvitation;
    pendingInviteText;
    settings;
    logout;
    buttonSkipCustomization;
    buttonCloseHelpCentre;
    collaboratorName;
    inviteModal;
    constructor(page) {
        this.page = page;
        this.buttonAddUser = page.locator('[data-test="add-collaborator"]');
        this.inputFieldUserEmailAddress = page.locator('[data-test="multi-select-input"]');
        this.buttonSendInvitation = page.locator('[data-test="invite-email-button"]');
        this.pendingInviteText = page.getByText('pending').first();
        this.settings = page.getByLabel('Open Settings');
        this.logout = page.getByRole('button', { name: 'Log out' });
        this.buttonSkipCustomization = page.getByRole('button', {
            name: 'Skip customization',
        });
        this.buttonCloseHelpCentre = page.getByRole('button', { name: 'Close' });
        this.collaboratorName = page.getByText('QA Camunda');
        this.inviteModal = this.page.locator('text="Invite collaborators to"');
    }
    async clickAddUser() {
        if (await this.inviteModal.isVisible({ timeout: 2000 })) {
            console.log('Invite modal already open, skipping Add user click');
            return;
        }
        await (0, test_1.expect)(this.buttonAddUser).toBeVisible({ timeout: 60000 });
        try {
            await this.buttonAddUser.click({ timeout: 10000 });
        }
        catch (error) {
            console.log('Regular click failed, trying force click');
            await this.buttonAddUser.click({ force: true });
        }
    }
    async clickUserEmailInput() {
        await (0, test_1.expect)(this.inputFieldUserEmailAddress).toBeVisible({
            timeout: 60000,
        });
        await this.inputFieldUserEmailAddress.click();
    }
    async fillUserEmailInput(emailAddress) {
        await this.inputFieldUserEmailAddress.fill(emailAddress);
        await (0, test_1.expect)(this.inputFieldUserEmailAddress).toHaveValue(emailAddress);
        await this.inputFieldUserEmailAddress.press('Enter');
    }
    async clickSendInviteButton() {
        await (0, test_1.expect)(this.buttonSendInvitation).toBeVisible({ timeout: 60000 });
        await this.buttonSendInvitation.click();
    }
    async checkPendingInviteText(email) {
        const maxRetries = 4;
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            try {
                await (0, test_1.expect)(this.page.getByRole('row').filter({ hasText: `${email}` })).toContainText('pending', {
                    timeout: 30000,
                });
                return;
            }
            catch (error) {
                if (attempt === 0) {
                    await this.page.reload();
                    await (0, sleep_1.sleep)(20000);
                }
                else if (attempt === 1 || attempt === 2) {
                    await this.inviteNewUser(email);
                }
                else {
                    console.error(error);
                    throw new Error(`Inviting a user failed after ${maxRetries} attempts`);
                }
            }
        }
        await (0, test_1.expect)(this.page.getByRole('row').filter({ hasText: `${email}` })).toContainText('pending', {
            timeout: 120000,
        });
    }
    async inviteNewUser(email) {
        await this.clickAddUser();
        await this.clickUserEmailInput();
        await this.fillUserEmailInput(email);
        await (0, sleep_1.sleep)(8000);
        await this.clickSendInviteButton();
    }
    async clickSettings() {
        await (0, test_1.expect)(this.settings).toBeVisible({ timeout: 60000 });
        await this.settings.click({ timeout: 90000 });
    }
    async clickLogout() {
        await (0, test_1.expect)(this.logout).toBeVisible({ timeout: 60000 });
        await this.logout.click({ timeout: 50000 });
    }
    async clickSkipCustomization() {
        await (0, test_1.expect)(this.buttonSkipCustomization).toBeVisible({ timeout: 60000 });
        await this.buttonSkipCustomization.click();
    }
    async clickCloseHelpCenter() {
        await (0, test_1.expect)(this.buttonCloseHelpCentre).toBeVisible({ timeout: 60000 });
        await this.buttonCloseHelpCentre.click();
    }
    async verifyCollaboratorName() {
        await (0, test_1.expect)(this.collaboratorName).toBeVisible({ timeout: 60000 });
    }
}
exports.ModelerUserInvitePage = ModelerUserInvitePage;
