"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskPanelPage = void 0;
const test_1 = require("@playwright/test");
const constants_1 = require("../../utils/constants");
const sleep_1 = require("../../utils/sleep");
class TaskPanelPage {
    page;
    availableTasks;
    filterOptions;
    tasklistBanner;
    collapseFilter;
    processesPageTab;
    tasksTab;
    assignToMeButton;
    unassignButton;
    licenseKeyTagNonProduction;
    licenseKeyTagProduction;
    constructor(page) {
        this.page = page;
        this.availableTasks = page.getByTitle('Available tasks');
        this.filterOptions = page
            .locator('[aria-label="Filter controls"] li')
            .filter({ hasText: 'Expand to show filters' });
        this.tasklistBanner = page.getByRole('link', {
            name: 'Camunda logo Tasklist',
        });
        this.collapseFilter = page.locator('button[aria-controls="task-nav-bar"][aria-expanded="true"]');
        this.processesPageTab = page.getByRole('link', { name: 'Processes' });
        this.tasksTab = page.getByRole('link', { name: 'Tasks', exact: true });
        this.assignToMeButton = page.getByRole('button', { name: 'Assign to me' });
        this.unassignButton = page.getByRole('button', { name: 'Unassign' });
        this.licenseKeyTagNonProduction = page.getByText('Non-production license', {
            exact: true,
        });
        this.licenseKeyTagProduction = page.getByText('Production license', {
            exact: true,
        });
    }
    async openTask(name, assignButton = true) {
        const maxAttempts = 10;
        for (let attempt = 1; attempt <= maxAttempts; attempt++) {
            try {
                await this.page.waitForLoadState('domcontentloaded');
                await this.availableTasks
                    .getByText(name, { exact: true })
                    .nth(0)
                    .click({ timeout: constants_1._1_SECOND_IN_MS * 4 });
                if (assignButton) {
                    await (0, test_1.expect)(this.assignToMeButton).toBeVisible({
                        timeout: constants_1._1_SECOND_IN_MS * 10,
                    });
                }
                return;
            }
            catch (error) {
                if (attempt < maxAttempts) {
                    console.warn(`Attempt ${attempt} failed. Reloading page...`);
                    await this.page.reload();
                    await this.page.waitForLoadState('domcontentloaded');
                }
                else {
                    throw new Error(`Failed to open task "${name}" after ${maxAttempts} attempts: ${error}`);
                }
            }
        }
    }
    async filterBy(option) {
        await this.filterOptions.click({ timeout: 45000 });
        await (0, test_1.expect)(this.page.getByRole('link', { name: option })).toBeVisible({
            timeout: 120000,
        });
        await this.page.getByRole('link', { name: option }).click({ timeout: 120000 });
    }
    async scrollToLastTask(name) {
        await this.page.getByText(name).last().scrollIntoViewIfNeeded();
    }
    async scrollToFirstTask(name) {
        await this.page.getByText(name).first().scrollIntoViewIfNeeded();
    }
    async clickCollapseFilter() {
        await this.collapseFilter.click({ timeout: 45000 });
    }
    async clickProcessesTab() {
        await this.processesPageTab.click();
    }
    async taskCount(name) {
        await (0, sleep_1.sleep)(5000);
        return this.availableTasks.getByText(name, { exact: true }).count();
    }
    async clickTasksTab() {
        await this.tasksTab.click();
    }
}
exports.TaskPanelPage = TaskPanelPage;
