"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;
    collaboratorEmail;
    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.collaboratorEmail = (email) => this.page.locator('[data-test="chip"]').filter({ hasText: `${email}` });
        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();
        await (0, test_1.expect)(this.page.getByText('Invite has been sent')).toBeVisible({
            timeout: 60000,
        });
    }
    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`);
                }
            }
        }
    }
    async inviteNewUser(email) {
        await this.clickAddUser();
        await this.clickUserEmailInput();
        await this.fillUserEmailInput(email);
        await (0, sleep_1.sleep)(8000);
        await this.clickSendInviteButton();
    }
    async fillNewUserWithRetry(email) {
        const maxRetries = 3;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                console.log('Adding new user to project:', email);
                await this.clickUserEmailInput();
                await this.fillUserEmailInput(email);
                await (0, test_1.expect)(this.collaboratorEmail(email).first()).toBeVisible({
                    timeout: 20000,
                });
                return;
            }
            catch (error) {
                console.log(`Attempt ${attempt}: Error filling email input:${email}`, error);
            }
            if (attempt < maxRetries) {
                await this.inputFieldUserEmailAddress.clear();
                await (0, sleep_1.sleep)(1000);
            }
        }
        throw new Error(`Failed to filling email input:${email} after ${maxRetries} attempts`);
    }
    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;
