"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;
    createNewDropdownButton;
    createExtractionTemplateOption;
    createClassificationTemplateOption;
    idpExtractionTemplateName;
    idpClassificationTemplateName;
    idpExtractionDescriptionField;
    idpClassificationDescriptionField;
    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;
    selectStructuredDataExtractionLabel;
    extractionInProgressNotification;
    extractionInProgressNotificationForMultipleFiles;
    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;
    idpClassificationAddDocumentTypeButton;
    idpClassificationInvoiceTypeOption;
    idpClassificationBankStatementTypeOption;
    addDocumentsTypesButton;
    classificationUploadFilesButton;
    selectDocumentDropdown;
    selectFilesButton;
    classificationRunTestButton;
    classificationTestResultsPanel;
    classificationModelName;
    classificationAccuracyValue;
    classifyingLoader;
    classificationSuccessfullPublishNotification;
    constructor(page) {
        this.page = page;
        this.createNewDropdownButton = page.locator('[data-test="idp-create-template-dropdown"]');
        this.createExtractionTemplateOption = page.locator('[data-test="create-extraction-template"]');
        this.createClassificationTemplateOption = page.locator('[data-test="create-classification-template"]');
        this.idpExtractionTemplateName = page.locator('#idp-project-name');
        this.idpClassificationTemplateName = page.locator('#idp-classification-template-name');
        this.idpExtractionDescriptionField = page
            .getByRole('region', { name: 'Create extraction template' })
            .locator('#idp-project-description');
        this.idpClassificationDescriptionField = page
            .getByRole('region', { name: 'Create classification template' })
            .locator('#idp-project-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.locator('#idp-project-extraction-method-unstructured');
        this.selectStructuredDataExtraction = page.locator('#idp-project-extraction-method-structured');
        this.selectStructuredDataExtractionLabel = page.locator('label[for="idp-project-extraction-method-structured"]');
        this.extractionInProgressNotification = page.getByText('Extracting document...');
        this.extractionInProgressNotificationForMultipleFiles = page.getByText('Extracting documents...');
        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' });
        this.idpClassificationAddDocumentTypeButton = page.locator('[data-test="add-document-type-empty"]');
        this.idpClassificationInvoiceTypeOption = page.getByRole('button', {
            name: 'Invoice Business invoices and',
        });
        this.idpClassificationBankStatementTypeOption = page.getByRole('button', {
            name: 'Bank Statement Financial',
        });
        this.addDocumentsTypesButton = page.getByRole('button', {
            name: 'Add types',
        });
        this.classificationUploadFilesButton = page.getByRole('button', {
            name: 'Upload files',
        });
        this.selectDocumentDropdown = page.locator('#document-type-dropdown');
        this.selectFilesButton = page.getByRole('button', { name: 'Select files' });
        this.classificationRunTestButton = page.getByRole('button', {
            name: 'Run Test',
        });
        this.classificationTestResultsPanel = page.locator('[data-test="test-template-tab-panel"]');
        this.classifyingLoader = page.getByText('Classifying...');
        this.classificationSuccessfullPublishNotification = page
            .getByRole('alert')
            .filter({ hasText: 'IDP project published successfully.' });
        const modelResultsRegion = this.classificationTestResultsPanel.getByRole('region', { name: 'Model results' });
        this.classificationModelName = modelResultsRegion
            .locator('.cds--tile span[title]')
            .first();
        this.classificationAccuracyValue = modelResultsRegion
            .locator('.cds--tile')
            .first()
            .locator('span:has-text("Accuracy") + span');
    }
    async clickCreateIdpTemplateButton() {
        await this.createNewDropdownButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.createExtractionTemplateOption).toBeVisible({
            timeout: 10000,
        });
        await this.createExtractionTemplateOption.click({ timeout: 60000 });
    }
    async fillIdpExtractionTemplateName(name) {
        await this.idpExtractionTemplateName.fill(name);
    }
    async clickCreateIdpClassificationTemplateButton() {
        await this.createNewDropdownButton.click({ timeout: 60000 });
        await (0, test_1.expect)(this.createClassificationTemplateOption).toBeVisible({
            timeout: 10000,
        });
        await this.createClassificationTemplateOption.click({ timeout: 60000 });
    }
    async fillIdpClassificationTemplateName(name) {
        await this.idpClassificationTemplateName.fill(name);
    }
    async clickCreateIdpExtractationTemplateButton() {
        await this.clickCreateTemplateButton();
    }
    async clickCreateTemplateButton() {
        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: 120000,
        });
        await (0, test_1.expect)(this.unstructured_saveAsTestCaseButton).toBeVisible({
            timeout: 120000,
        });
        await (0, test_1.expect)(this.unstructured_saveAsTestCaseButton).toBeEnabled({
            timeout: 120000,
        });
    }
    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 (0, test_1.expect)(this.selectStructuredDataExtractionLabel).toBeVisible({
            timeout: 30000,
        });
        const maxRetries = 3;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            await this.selectStructuredDataExtractionLabel.click({ timeout: 60000 });
            const isChecked = await this.selectStructuredDataExtraction.isChecked();
            if (isChecked)
                return;
            console.warn(`Structured extraction not selected on attempt ${attempt}, retrying...`);
            await this.page.waitForTimeout(1000);
        }
        await (0, test_1.expect)(this.selectStructuredDataExtraction).toBeChecked({
            timeout: 5000,
        });
    }
    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: 120000,
        });
        await (0, test_1.expect)(this.structured_FieldsTab).toBeVisible({
            timeout: 180000,
        });
        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.extractionInProgressNotificationForMultipleFiles).not.toBeVisible({
            timeout: 150000,
        });
    }
    async extractionResultsAreVisible() {
        await (0, test_1.expect)(this.structured_TestSummaryResult).toBeVisible({
            timeout: 120000,
        });
        await (0, test_1.expect)(this.structured_DetailsResult).toBeVisible({
            timeout: 60000,
        });
    }
    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 fillIdpExtractionDescriptionField(description) {
        await this.idpExtractionDescriptionField.fill(description);
    }
    async fillIdpClassificationDescriptionField(description) {
        await this.idpClassificationDescriptionField.fill(description);
    }
    async clickAddDocumentTypeButton() {
        await (0, test_1.expect)(this.idpClassificationAddDocumentTypeButton).toBeVisible();
        await this.idpClassificationAddDocumentTypeButton.click();
    }
    async clickInvoiceTypeOption() {
        await (0, test_1.expect)(this.idpClassificationInvoiceTypeOption).toBeVisible();
        await this.idpClassificationInvoiceTypeOption.click();
    }
    async clickBankStatementTypeOption() {
        await (0, test_1.expect)(this.idpClassificationBankStatementTypeOption).toBeVisible();
        await this.idpClassificationBankStatementTypeOption.click();
    }
    async clickAddTypesButton() {
        await (0, test_1.expect)(this.addDocumentsTypesButton).toBeVisible();
        await this.addDocumentsTypesButton.click();
    }
    async clickClassificationUploadFilesButton() {
        await (0, test_1.expect)(this.classificationUploadFilesButton).toBeVisible({
            timeout: 60000,
        });
        await this.classificationUploadFilesButton.click();
        await (0, test_1.expect)(this.selectDocumentDropdown).toBeVisible({ timeout: 30000 });
    }
    async clickSelectDocumentDropdown() {
        await (0, test_1.expect)(this.selectDocumentDropdown).toBeVisible();
        await this.selectDocumentDropdown.click();
    }
    async selectDocumentTypeOption(documentType) {
        const option = this.page
            .locator('[data-test="test-template-tab-panel"]')
            .getByText(new RegExp(`^${documentType}$`));
        await (0, test_1.expect)(option).toBeVisible();
        await option.click();
    }
    async clickSelectFilesButton() {
        await (0, test_1.expect)(this.selectFilesButton).toBeVisible();
        await this.selectFilesButton.click();
    }
    async clickClassificationRunTestButton() {
        await (0, test_1.expect)(this.classificationRunTestButton).toBeEnabled({
            timeout: 60000,
        });
        await this.classificationRunTestButton.click();
    }
    async assertClassificationTestResultsArePresent() {
        await (0, test_1.expect)(this.classificationTestResultsPanel).toBeVisible();
    }
    async assertClassificationModelName(expectedModel) {
        await (0, test_1.expect)(this.classificationModelName).toBeVisible({ timeout: 60000 });
        await (0, test_1.expect)(this.classificationModelName).toHaveText(expectedModel);
    }
    async assertClassificationAccuracyIsPresent() {
        await (0, test_1.expect)(this.classificationAccuracyValue).toBeVisible({
            timeout: 60000,
        });
        await (0, test_1.expect)(this.classificationAccuracyValue).toHaveText(/^\d+%$/);
    }
    async classifyingProgressIsComplete() {
        await (0, test_1.expect)(this.classifyingLoader).not.toBeVisible({ timeout: 300000 });
    }
    async assertClassificationTemplateSuccessNotification() {
        await (0, test_1.expect)(this.classificationSuccessfullPublishNotification).toBeVisible();
    }
    async selectProvider(provider) {
        await (0, test_1.expect)(async () => {
            await this.providerDropdown.click({ timeout: 10000 });
            await (0, test_1.expect)(this.providerDropdown).toHaveAttribute('aria-expanded', 'true', { timeout: 5000 });
        }).toPass({ timeout: 60000 });
        const providerOption = this.page
            .locator('[role="listbox"] [role="option"]')
            .filter({ hasText: new RegExp(`^${provider}$`) });
        await (0, test_1.expect)(providerOption).toBeVisible({ timeout: 120000 });
        await providerOption.click({ timeout: 120000 });
    }
}
exports.IdpCreatePage = IdpCreatePage;
