"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskPanelPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const clickLocatorWithRetry_1 = require("../../utils/assertionHelpers/clickLocatorWithRetry");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
class TaskPanelPage {
    page;
    availableTasks;
    filterPanel;
    taskListPageBanner;
    processesPageTab;
    tasksTab;
    noPermissionsMessage;
    expandButton;
    processesInfoText;
    constructor(page) {
        this.page = page;
        this.availableTasks = page.getByTitle('Available tasks');
        this.filterPanel = page.locator('[aria-controls="task-nav-bar"]');
        this.taskListPageBanner = page.getByRole('link', {
            name: 'Camunda logo Tasklist',
        });
        this.processesPageTab = page.getByRole('link', { name: 'Processes' });
        this.tasksTab = page.getByRole('link', { name: 'Tasks' });
        this.noPermissionsMessage = page.getByText('You don’t have access to this component');
        this.expandButton = page.getByRole('button', {
            name: 'Expand to show filters',
        });
        this.processesInfoText = page.getByText('Browse and run processes published by your organization');
    }
    async openTask(name) {
        let attempts = 0;
        const maxAttempts = 3;
        while (attempts < maxAttempts) {
            try {
                const task = this.availableTasks.getByText(name, { exact: true }).first();
                await task.waitFor({ state: 'visible', timeout: 120000 });
                await task.click({ timeout: 120000 });
                return;
            }
            catch (error) {
                attempts++;
                if (attempts >= maxAttempts) {
                    throw error;
                }
                await this.page.reload();
            }
        }
    }
    async filterBy(option) {
        try {
            await this.filterPanel.click({ timeout: 60000 });
            await (0, test_1.expect)(this.page.getByRole('link', { name: option }).getByText(option)).toBeVisible({ timeout: 60000 });
            await this.page
                .getByRole('link', { name: option })
                .getByText(option)
                .click({ timeout: 60000 });
        }
        catch {
            await this.expandButton.click({ timeout: 60000 });
            await this.page
                .getByRole('link', { name: option })
                .getByText(option)
                .click({ timeout: 60000 });
        }
    }
    async taskCount(name) {
        await (0, sleep_1.sleep)(20000);
        return this.availableTasks.getByText(name, { exact: true }).count();
    }
    async clickTasksTab() {
        await (0, test_1.expect)(this.tasksTab).toBeVisible({ timeout: 60000 });
        await this.tasksTab.click({ timeout: 60000 });
    }
    async scrollToLastTask(name) {
        await this.page.getByText(name).last().scrollIntoViewIfNeeded();
    }
    async scrollToFirstTask(name) {
        await this.page.getByText(name).first().scrollIntoViewIfNeeded();
    }
    async clickProcessesTab() {
        await (0, clickLocatorWithRetry_1.clickLocatorWithRetry)(this.page, this.processesPageTab);
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.processesInfoText, {
            preAction: async () => {
                await (0, test_1.expect)(this.processesPageTab).toBeVisible({ timeout: 10000 });
                await this.processesPageTab.click({ timeout: 10000 });
            },
        });
    }
}
exports.TaskPanelPage = TaskPanelPage;
