"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;
    idpDescriptionField;
    providerDropdown;
    createIdpExtractationTemplateButton;
    unstructured_UploadFilesButton;
    extractButton;
    unstructured_extractFieldName;
    unstructured_extractFieldPrompt;
    unstructured_addExtractionFieldButton;
    unstructured_extractionModelDropdown;
    unstructured_extractDocumentButton;
    unstructured_saveAsTestCaseButton;
    unstructured_testCaseSavedNotification;
    unstructured_validateExtractionTab;
    unstructured_expandAllRowsButton;
    unstructured_passTestCaseResultsRows;
    unstructured_publishButton;
    unstructured_publishToProjecOption;
    unstructured_versionFieldName;
    unstructured_versionFieldDescription;
    unstructured_publishButtonModal;
    unstructured_idpSuccessfullPublishNotification;
    unstructured_toastNotificationCloseButton;
    selectUnstructuredDataExtraction;
    selectStructuredDataExtraction;
    extractionInProgressNotification;
    structured_FieldsTab;
    structured_TablesTab;
    structured_CheckboxSelectAllFields;
    structured_CheckboxSelectAllTables;
    structured_uploadFilesButton;
    structured_SelectedFieldsTablesMessage;
    structured_TestTemplateTab;
    structured_TestExtractionTemplateButton;
    structured_TestSummaryResult;
    structured_DetailsResult;
    structured_PublishTemplateButton;
    structured_PublishTemplateToProjectButton;
    structured_SuccessfullPublishNotification;
    constructor(page) {
        this.page = page;
        this.createIdpTemplateButton = page.getByRole('button', {
            name: 'Create extraction template',
            exact: true,
        });
        this.idpExtractionTemplateName = page.locator('#idp-project-name');
        this.idpDescriptionField = page.getByLabel('Description');
        this.createIdpExtractationTemplateButton = page.getByRole('button', {
            name: 'Create template',
            exact: true,
        });
        this.unstructured_UploadFilesButton = page.getByRole('button', {
            name: 'Drag and drop PDF files here',
        });
        this.extractButton = page.getByRole('button', {
            name: 'Extract',
            exact: true,
        });
        this.unstructured_extractFieldName = page.locator('#new-extraction-field-name');
        this.unstructured_extractFieldPrompt = page.locator('#new-extraction-field-prompt');
        this.unstructured_addExtractionFieldButton = page.getByRole('button', {
            name: 'Add',
            exact: true,
        });
        this.unstructured_extractionModelDropdown =
            page.locator('#extraction-model');
        this.unstructured_extractDocumentButton = page.getByRole('button', {
            name: 'Extract document',
        });
        this.unstructured_saveAsTestCaseButton = page.getByRole('button', {
            name: 'Save as test case',
        });
        this.unstructured_validateExtractionTab = page.getByRole('tab', {
            name: 'Validate extraction',
        });
        this.unstructured_testCaseSavedNotification = page.getByText('NotificationTest case');
        this.unstructured_expandAllRowsButton = page.getByLabel('Expand all rows');
        this.unstructured_passTestCaseResultsRows = page.locator('[data-test="pass-test-case-results"]');
        this.unstructured_publishButton = page.getByRole('button', {
            name: 'Publish',
        });
        this.unstructured_publishToProjecOption = page.getByRole('menuitem', {
            name: 'Publish to project',
        });
        this.unstructured_versionFieldName = page.locator('#version-name');
        this.unstructured_versionFieldDescription = page.locator('#version-description');
        this.unstructured_publishButtonModal = page
            .getByLabel('Publish Extraction Project')
            .getByRole('button', { name: 'Publish' });
        this.unstructured_idpSuccessfullPublishNotification = page.getByText('NotificationSuccessIDP');
        this.unstructured_toastNotificationCloseButton =
            page.getByLabel('Close notification');
        this.selectUnstructuredDataExtraction = page.getByText('Unstructured data extraction');
        this.selectStructuredDataExtraction = page.getByText('Structured form extraction');
        this.extractionInProgressNotification = page.getByText('Extracting document...');
        this.structured_uploadFilesButton = page
            .getByRole('button', {
            name: /Drag and drop.*PDF.*upload/i,
        })
            .first();
        this.structured_FieldsTab = page.getByRole('tab', {
            name: 'Fields',
        });
        this.structured_TablesTab = page.getByRole('tab', {
            name: 'Tables',
        });
        this.structured_TestTemplateTab = page.getByRole('tab', {
            name: 'Test template',
        });
        this.structured_CheckboxSelectAllFields = page.getByRole('columnheader', {
            name: 'Activate all rows',
        });
        this.structured_CheckboxSelectAllTables =
            page.getByText('Select all tables');
        this.structured_SelectedFieldsTablesMessage = page.getByText(/^\s*\d+\s+fields\s*&\s*\d+\s+table(?:s)?\s+selected$/);
        this.structured_TestExtractionTemplateButton = page.getByRole('button', {
            name: 'Test extraction template',
        });
        this.structured_TestSummaryResult = page.getByRole('heading', {
            name: 'Test Summary Results',
        });
        this.structured_DetailsResult = page.getByRole('heading', {
            name: 'Detailed Results',
        });
        this.structured_PublishTemplateButton = page.getByRole('button', {
            name: 'Publish Template',
        });
        this.structured_PublishTemplateToProjectButton = page.getByRole('button', {
            name: 'Publish',
            exact: true,
        });
        this.structured_SuccessfullPublishNotification =
            page.getByText('Template published');
        this.providerDropdown = page.getByRole('combobox', { name: 'Provider' });
    }
    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 clickUnstructuredUploadFilesButton() {
        await this.unstructured_UploadFilesButton.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(6000);
    }
    async clickExtractButton() {
        await (0, test_1.expect)(this.extractButton).toBeEnabled({
            timeout: 80000,
        });
        await this.extractButton.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(6000);
    }
    async fillExtractFieldName(name) {
        await this.unstructured_extractFieldName.fill(name);
    }
    async fillPromptField() {
        await this.unstructured_extractFieldPrompt.fill('Provide me the account number from this document');
    }
    async clickAddExtractionFieldButton() {
        await this.unstructured_addExtractionFieldButton.click({ timeout: 60000 });
    }
    async selectExtractionModelOption(option) {
        const comboboxOption = this.page.getByRole('option', { name: option });
        if (await comboboxOption.isVisible()) {
            console.log(`Option ${option} is already visible, no need to click dropdown`);
        }
        else {
            await this.unstructured_extractionModelDropdown.click({
                timeout: 60000,
            });
            await comboboxOption.click({ timeout: 120000 });
        }
    }
    async clickExtractDocumentButton() {
        await (0, test_1.expect)(this.unstructured_extractDocumentButton).toBeVisible({
            timeout: 90000,
        });
        await this.unstructured_extractDocumentButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.page.getByText('Extracting..')).not.toBeVisible({
            timeout: 300000,
        });
        await (0, test_1.expect)(this.unstructured_saveAsTestCaseButton).toBeVisible({
            timeout: 300000,
        });
        await (0, test_1.expect)(this.unstructured_saveAsTestCaseButton).toBeEnabled({
            timeout: 300000,
        });
    }
    async clickSaveAsTestCaseButton() {
        await this.unstructured_saveAsTestCaseButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.unstructured_testCaseSavedNotification).toBeVisible({
            timeout: 120000,
        });
    }
    async clickValidateExtractionTab() {
        await this.unstructured_validateExtractionTab.click({ timeout: 60000 });
    }
    async clickExpandAllRowsButton() {
        await (0, test_1.expect)(this.unstructured_expandAllRowsButton).toBeVisible({
            timeout: 120000,
        });
        await this.unstructured_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.unstructured_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.unstructured_publishButton.click({ timeout: 60000 });
    }
    async clickPublishToProjecOption() {
        await this.unstructured_publishToProjecOption.click({ timeout: 60000 });
    }
    async fillVersionFieldName(name) {
        await this.unstructured_versionFieldName.fill(name);
    }
    async fillVersionFieldDescription(description) {
        await this.unstructured_versionFieldDescription.fill(description);
    }
    async clickPublishButtonModal() {
        await this.unstructured_publishButtonModal.click({ timeout: 60000 });
    }
    async assertIdpSuccessPublishNotification() {
        await (0, test_1.expect)(this.unstructured_idpSuccessfullPublishNotification).toBeVisible({
            timeout: 120000,
        });
    }
    async clickCloseToastNotification() {
        await this.unstructured_toastNotificationCloseButton.click({
            timeout: 60000,
        });
    }
    async selectStructuredDataExtractionMethod() {
        await this.selectStructuredDataExtraction.click();
    }
    async clickStructuredUploadFilesButton() {
        await (0, test_1.expect)(this.structured_uploadFilesButton).toBeVisible({
            timeout: 60000,
        });
        await this.structured_uploadFilesButton.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(6000);
    }
    async selectAllExtractionFields() {
        await (0, test_1.expect)(this.extractionInProgressNotification).not.toBeVisible({
            timeout: 300000,
        });
        await (0, test_1.expect)(this.structured_FieldsTab).toBeVisible({
            timeout: 300000,
        });
        await (0, test_1.expect)(this.structured_CheckboxSelectAllFields).toBeVisible({
            timeout: 60000,
        });
        await this.structured_CheckboxSelectAllFields.click({ timeout: 60000 });
    }
    async clickTablesTab() {
        await (0, test_1.expect)(this.structured_TablesTab).toBeVisible({
            timeout: 60000,
        });
        await this.structured_TablesTab.click({ timeout: 60000 });
    }
    async selectAllExtractedTables() {
        await (0, test_1.expect)(this.structured_CheckboxSelectAllTables).toBeVisible({
            timeout: 60000,
        });
        await this.structured_CheckboxSelectAllTables.click({ timeout: 60000 });
    }
    async assertFieldsAndTablesSelection() {
        await (0, test_1.expect)(this.structured_SelectedFieldsTablesMessage).toBeVisible({
            timeout: 60000,
        });
    }
    async clickTestTemplateTab() {
        await (0, test_1.expect)(this.structured_TestTemplateTab).toBeVisible({
            timeout: 60000,
        });
        await this.structured_TestTemplateTab.click({ timeout: 60000 });
    }
    async clickTestExtractionTemplateButton() {
        await (0, test_1.expect)(this.structured_TestExtractionTemplateButton).toBeEnabled({
            timeout: 60000,
        });
        await this.structured_TestExtractionTemplateButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.extractionInProgressNotification).not.toBeVisible({
            timeout: 300000,
        });
    }
    async extractionResultsAreVisible() {
        await (0, test_1.expect)(this.structured_TestSummaryResult).toBeVisible({
            timeout: 180000,
        });
        await (0, test_1.expect)(this.structured_DetailsResult).toBeVisible({
            timeout: 180000,
        });
    }
    async clickPublishTemplateButton() {
        await (0, test_1.expect)(this.structured_PublishTemplateButton).toBeVisible({
            timeout: 60000,
        });
        await this.structured_PublishTemplateButton.click({ timeout: 60000 });
    }
    async clickPublishTemplateToProjectButton() {
        await (0, test_1.expect)(this.structured_PublishTemplateToProjectButton).toBeVisible({
            timeout: 60000,
        });
        await this.structured_PublishTemplateToProjectButton.click({
            timeout: 60000,
        });
    }
    async assertStructuredTemplateSuccessNotification() {
        await (0, test_1.expect)(this.structured_SuccessfullPublishNotification).toBeVisible({
            timeout: 120000,
        });
    }
    async fillIdpDescriptionField(description) {
        await this.idpDescriptionField.fill(description);
    }
    async selectProvider(provider) {
        await this.providerDropdown.click({ timeout: 60000 });
        const providerOption = this.page
            .getByRole('option', { name: provider })
            .getByText(provider);
        await (0, test_1.expect)(providerOption).toBeVisible({ timeout: 120000 });
        await providerOption.click({ timeout: 120000 });
    }
}
exports.IdpCreatePage = IdpCreatePage;
