"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdpCreatePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class IdpCreatePage {
    page;
    createIdpTemplateButton;
    idpExtractionTemplateName;
    createIdpExtractationTemplateButton;
    uploadFilesButton;
    extractButton;
    extractFieldName;
    extractFieldPrompt;
    addExtractionFieldButton;
    extractionModelDropdown;
    extractDocumentButton;
    extractingLoader;
    saveAsTestCaseButton;
    testCaseSavedNotification;
    validateExtractionTab;
    expandAllRowsButton;
    passTestCaseResultsRows;
    publishButton;
    publishToProjecOption;
    versionFieldName;
    versionFieldDescription;
    publishButtonModal;
    idpSuccessfullPublishNotification;
    toastNotificationCloseButton;
    constructor(page) {
        this.page = page;
        this.createIdpTemplateButton = page.getByRole('button', {
            name: 'Create extraction template',
            exact: true,
        });
        this.idpExtractionTemplateName = page.locator('#idp-project-name');
        this.createIdpExtractationTemplateButton = page.getByRole('button', {
            name: 'Create template',
            exact: true,
        });
        this.uploadFilesButton = page.getByRole('button', {
            name: 'Upload documents',
        });
        this.extractButton = page.getByRole('button', {
            name: 'Extract',
            exact: true,
        });
        this.extractFieldName = page.locator('#new-extraction-field-name');
        this.extractFieldPrompt = page.locator('#new-extraction-field-prompt');
        this.addExtractionFieldButton = page.getByRole('button', {
            name: 'Add',
            exact: true,
        });
        this.extractionModelDropdown = page.locator('#extraction-model');
        this.extractDocumentButton = page.getByRole('button', {
            name: 'Extract document',
        });
        this.extractingLoader = page.getByText('Extracting...');
        this.saveAsTestCaseButton = page.getByRole('button', {
            name: 'Save as test case',
        });
        this.validateExtractionTab = page.getByRole('tab', {
            name: 'Validate extraction',
        });
        this.testCaseSavedNotification = page.getByText('NotificationTest case');
        this.expandAllRowsButton = page.getByLabel('Expand all rows');
        this.passTestCaseResultsRows = page.locator('[data-test="pass-test-case-results"]');
        this.publishButton = page.getByRole('button', {
            name: 'Publish',
        });
        this.publishToProjecOption = page.getByRole('menuitem', {
            name: 'Publish to project',
        });
        this.versionFieldName = page.locator('#version-name');
        this.versionFieldDescription = page.locator('#version-description');
        this.publishButtonModal = page
            .getByLabel('Publish Extraction Project')
            .getByRole('button', { name: 'Publish' });
        this.idpSuccessfullPublishNotification = page.getByText('NotificationSuccessIDP');
        this.toastNotificationCloseButton = page.getByLabel('Close notification');
    }
    async clickCreateIdpTemplateButton() {
        await this.createIdpTemplateButton.click({ timeout: 60000 });
    }
    async fillIdpExtractionTemplateName(name) {
        await this.idpExtractionTemplateName.fill(name);
    }
    async clickCreateIdpExtractationTemplateButton() {
        await this.createIdpExtractationTemplateButton.click({
            timeout: 60000,
        });
    }
    async clickUploadFilesButton() {
        await this.uploadFilesButton.click({ timeout: 60000 });
    }
    async clickExtractButton() {
        await this.extractButton.click({ timeout: 60000 });
    }
    async fillExtractFieldName(name) {
        await this.extractFieldName.fill(name);
    }
    async fillPromptField() {
        await this.extractFieldPrompt.fill('Provide me the account number from this document');
    }
    async clickAddExtractionFieldButton() {
        await this.addExtractionFieldButton.click({ timeout: 60000 });
    }
    async selectExtractionModelOption(option) {
        await this.extractionModelDropdown.click({ timeout: 60000 });
        await this.page.getByText(option).click({ timeout: 120000 });
    }
    async clickExtractDocumentButton() {
        await (0, test_1.expect)(this.extractDocumentButton).toBeVisible({ timeout: 90000 });
        await this.extractDocumentButton.click({ timeout: 90000 });
        await (0, test_1.expect)(this.extractingLoader).not.toBeVisible({ timeout: 200000 });
        await (0, test_1.expect)(this.saveAsTestCaseButton).toBeVisible({
            timeout: 120000,
        });
    }
    async clickSaveAsTestCaseButton() {
        await this.saveAsTestCaseButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.testCaseSavedNotification).toBeVisible({
            timeout: 120000,
        });
    }
    async clickValidateExtractionTab() {
        await this.validateExtractionTab.click({ timeout: 60000 });
    }
    async clickExpandAllRowsButton() {
        await (0, test_1.expect)(this.expandAllRowsButton).toBeVisible({
            timeout: 120000,
        });
        await this.expandAllRowsButton.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(1000);
    }
    async assertPassTestCaseResultsRow(retries = 3) {
        for (let i = 0; i < retries; i++) {
            try {
                await (0, test_1.expect)(this.passTestCaseResultsRows).toBeVisible({
                    timeout: 60000,
                });
                return; // Click succeeded, exit the loop
            }
            catch (error) {
                console.error(`Attempt ${i + 1} to assert pass test case failed: ${error}`);
                await this.page.reload();
                await this.clickValidateExtractionTab();
                await this.clickExpandAllRowsButton();
            }
        }
        throw new Error(`Failed to assert pass test case after ${retries} retries`);
    }
    async clickPublishButton() {
        await this.publishButton.click({ timeout: 60000 });
    }
    async clickPublishToProjecOption() {
        await this.publishToProjecOption.click({ timeout: 60000 });
    }
    async fillVersionFieldName(name) {
        await this.versionFieldName.fill(name);
    }
    async fillVersionFieldDescription(description) {
        await this.versionFieldDescription.fill(description);
    }
    async clickPublishButtonModal() {
        await this.publishButtonModal.click({ timeout: 60000 });
    }
    async assertIdpSuccessPublishNotification() {
        await (0, test_1.expect)(this.idpSuccessfullPublishNotification).toBeVisible({
            timeout: 120000,
        });
    }
    async clickCloseToastNotification() {
        await this.toastNotificationCloseButton.click({ timeout: 60000 });
    }
}
exports.IdpCreatePage = IdpCreatePage;
