"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskDetailsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
function cardinalToOrdinal(numberValue) {
    const realOrderIndex = numberValue.toString();
    if (['11', '12', '13'].includes(realOrderIndex.slice(-2))) {
        return `${realOrderIndex}th`;
    }
    switch (realOrderIndex.slice(-1)) {
        case '1':
            return `${realOrderIndex}st`;
        case '2':
            return `${realOrderIndex}nd`;
        case '3':
            return `${realOrderIndex}rd`;
        default:
            return `${realOrderIndex}th`;
    }
}
class TaskDetailsPage {
    page;
    assignToMeButton;
    completeButton;
    unassignButton;
    assignee;
    completeTaskButton;
    addVariableButton;
    detailsPanel;
    detailsHeader;
    pendingTaskDescription;
    pickATaskHeader;
    emptyTaskMessage;
    nameInput;
    addressInput;
    ageInput;
    variablesTable;
    nameColumnHeader;
    valueColumnHeader;
    form;
    numberInput;
    incrementButton;
    decrementButton;
    dateInput;
    timeInput;
    checkbox;
    selectDropdown;
    tagList;
    detailsInfo;
    textInput;
    taskCompletedBanner;
    assignedToMeText;
    constructor(page) {
        this.page = page;
        this.assignToMeButton = page.getByRole('button', { name: 'Assign to me' });
        this.completeButton = page.getByRole('button', { name: 'Complete' });
        this.unassignButton = page.getByRole('button', { name: 'Unassign' });
        this.assignee = page.getByTestId('assignee');
        this.completeTaskButton = page.getByRole('button', { name: 'Complete Task' });
        this.addVariableButton = page.getByRole('button', { name: 'Add Variable' });
        this.detailsPanel = this.page.getByRole('complementary', {
            name: 'Task details right panel',
        });
        this.detailsHeader = page.getByTitle('Task details header');
        this.pendingTaskDescription = page.getByText('Pending task');
        this.pickATaskHeader = page.getByRole('heading', {
            name: 'Pick a task to work on',
        });
        this.emptyTaskMessage = page.getByRole('heading', {
            name: /task has no variables/i,
        });
        this.nameInput = page.getByLabel('Name*');
        this.addressInput = page.getByLabel('Address*');
        this.ageInput = page.getByLabel('Age');
        this.variablesTable = page.getByTestId('variables-table');
        this.nameColumnHeader = this.variablesTable.getByRole('columnheader', {
            name: 'Name',
        });
        this.valueColumnHeader = this.variablesTable.getByRole('columnheader', {
            name: 'Value',
        });
        this.form = page.getByTestId('embedded-form');
        this.numberInput = this.form.getByLabel('Number');
        this.incrementButton = page.getByRole('button', { name: 'Increment' });
        this.decrementButton = page.getByRole('button', { name: 'Decrement' });
        this.dateInput = page.getByPlaceholder('mm/dd/yyyy');
        this.timeInput = page.getByPlaceholder('hh:mm ?m');
        this.checkbox = this.form.getByLabel('Checkbox');
        this.selectDropdown = this.form.getByText('Select').last();
        this.tagList = page.getByPlaceholder('Search');
        this.detailsInfo = page.getByTestId('details-info');
        this.textInput = page.locator('[class="fjs-input"]');
        this.taskCompletedBanner = this.page.getByText('Task completed').first();
        this.assignedToMeText = page
            .getByTestId('assignee')
            .getByText('Assigned to me');
    }
    async clickAssignToMeButton() {
        await (0, test_1.expect)(this.assignToMeButton).toBeVisible({ timeout: 60000 });
        await this.assignToMeButton.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(1000);
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.assignedToMeText, {
            postAction: async () => {
                await this.page.reload();
                await (0, sleep_1.sleep)(5000);
                if (await this.assignToMeButton.isVisible()) {
                    await this.assignToMeButton.click({ timeout: 60000 });
                }
            },
        });
    }
    async clickUnassignButton() {
        await this.unassignButton.click();
    }
    async clickCompleteTaskButton() {
        await this.completeTaskButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.page.getByText('Completing task...')).not.toBeVisible({
            timeout: 60000,
        });
        await (0, sleep_1.sleep)(1000);
        await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.taskCompletedBanner, {
            postAction: async () => {
                await this.page.reload();
                await (0, sleep_1.sleep)(5000);
                if (await this.completeTaskButton.isVisible()) {
                    await this.completeTaskButton.click({ timeout: 60000 });
                }
            },
        });
    }
    async clickAddVariableButton() {
        await this.addVariableButton.click({ timeout: 60000 });
    }
    async replaceExistingVariableValue(values) {
        const { name, value } = values;
        await this.page.getByTitle(name).clear();
        await this.page.getByTitle(name).fill(value);
    }
    getNthVariableNameInput(nth) {
        return this.page.getByRole('textbox', {
            name: `${cardinalToOrdinal(nth)} variable name`,
        });
    }
    getNthVariableValueInput(nth) {
        return this.page.getByRole('textbox', {
            name: `${cardinalToOrdinal(nth)} variable value`,
        });
    }
    async addVariable(payload) {
        const { name, value } = payload;
        this.clickAddVariableButton();
        await this.getNthVariableNameInput(1).fill(name);
        await this.getNthVariableValueInput(1).fill(value);
    }
    async fillNumber(number) {
        await this.numberInput.fill(number);
    }
    async clickIncrementButton() {
        await this.incrementButton.click();
    }
    async clickDecrementButton() {
        await this.decrementButton.click();
    }
    async fillDate(date) {
        await this.dateInput.click();
        await this.dateInput.fill(date);
        await this.dateInput.press('Enter');
    }
    async enterTime(time) {
        await this.timeInput.click();
        await this.page.getByText(time).click();
    }
    async checkCheckbox() {
        await this.checkbox.check();
    }
    async selectDropdownValue(value) {
        await this.selectDropdown.click();
        await this.page.getByText(value).click();
    }
    async clickRadioButton(radioBtnLabel) {
        await this.page.getByText(radioBtnLabel).click();
    }
    async checkChecklistBox(label) {
        await this.page.getByLabel(label).check();
    }
    async enterTwoValuesInTagList(value1, value2) {
        await this.tagList.click();
        await this.page.getByText(value1).click();
        await this.page.getByText(value2, { exact: true }).click();
    }
    async clickTextInput() {
        await this.textInput.click();
    }
    async fillTextInput(value) {
        await this.textInput.fill(value);
    }
    async priorityAssertion(priority) {
        let retryCount = 0;
        const maxRetries = 2;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.detailsPanel.getByText(priority)).toBeVisible({
                    timeout: 45000,
                });
                return; // Exit the function if the expectation is met
            }
            catch (error) {
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
                const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
                await sleep(10000);
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
    async taskAssertion(name) {
        let retryCount = 0;
        const maxRetries = 2;
        while (retryCount < maxRetries) {
            try {
                await (0, test_1.expect)(this.detailsInfo.getByText(name)).toBeVisible({
                    timeout: 45000,
                });
                return; // Exit the function if the expectation is met
            }
            catch (error) {
                retryCount++;
                console.log(`Attempt ${retryCount} failed. Retrying...`);
                await this.page.reload();
                const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
                await sleep(10000);
            }
        }
        throw new Error(`Active icon not visible after ${maxRetries} attempts.`);
    }
}
exports.TaskDetailsPage = TaskDetailsPage;
