"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_8_1 = require("../../fixtures/8.8");
const _setup_1 = require("../../test-setup.js");
const UtilitiesPage_1 = require("../../pages/8.8/UtilitiesPage");
const randomName_1 = require("../../utils/randomName");
const fileUpload_1 = require("../../utils/fileUpload");
const connectorSecrets_1 = require("../../utils/connectorSecrets");
const users_1 = require("../../utils/users");
const testUser = (0, users_1.getTestUser)('twentiethUser');
let idpApplicationName;
let idpDocumentExtractionTemplateName;
const unstructuredTestCases = [
    {
        provider: 'AWS',
        model: 'Claude Sonnet 4',
        uploadFiles: ['idp/idp_sample_bank_statement.pdf'],
    },
    {
        provider: 'Azure',
        model: 'OpenAI Model Router',
        uploadFiles: ['idp/idp_sample_bank_statement.pdf'],
    },
    {
        provider: 'GCP',
        model: 'Gemini 2.5 Pro',
        uploadFiles: ['idp/idp_sample_bank_statement.pdf'],
    },
];
const structuredTestCases = [
    {
        provider: 'AWS',
        model: '',
        uploadFiles: ['idp/structured_invoice_test.pdf'],
        extractionFiles: [
            'idp/structured_invoice_1.pdf',
            'idp/structured_invoice_2.pdf',
            'idp/structured_invoice_3.pdf',
        ],
    },
    {
        provider: 'GCP',
        model: '',
        uploadFiles: ['idp/structured_invoice_test.pdf'],
        extractionFiles: [
            'idp/structured_invoice_1.pdf',
            'idp/structured_invoice_2.pdf',
            'idp/structured_invoice_3.pdf',
        ],
    },
]; /* For structured documents, the model is empty since is not necessary */
_8_8_1.test.describe.configure({ mode: 'parallel' });
_8_8_1.test.describe('IDP User Flow Tests', () => {
    const clusterName = 'Test Cluster';
    _8_8_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    _8_8_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_8_1.test.describe('Unstructured Extraction', () => {
        for (const params of unstructuredTestCases) {
            (0, _8_8_1.test)(`${params.provider} unstructured (${params.model}) - User creates an idp application with a document extraction template`, async ({ page, idpCreatePage, modelerHomePage, homePage, appsPage, clusterPage, clusterSecretsPage, }) => {
                _8_8_1.test.slow();
                idpApplicationName = await (0, randomName_1.randomNameAgregator)(`${params.provider} unstructured` + ' - IDP_Application_Name');
                idpDocumentExtractionTemplateName = await (0, randomName_1.randomNameAgregator)(`${params.provider} unstructured` + ' - IDP_Extraction_Template_Name');
                await _8_8_1.test.step('Navigate to Connectors Secrets', async () => {
                    await homePage.clickClusters();
                    await clusterPage.clickClusterLink(clusterName);
                    await clusterPage.clickConnectorSecretsTab(clusterName);
                    await clusterSecretsPage.assertSecretsOrCreate(clusterName, connectorSecrets_1.allConnectorSecrets[params.provider]);
                });
                await _8_8_1.test.step('Navigate to Web Modeler', async () => {
                    await (0, test_1.expect)(homePage.camundaComponentsButton).toBeVisible({
                        timeout: 120000,
                    });
                    await appsPage.clickCamundaApps();
                    await appsPage.clickModeler();
                    await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                        timeout: 120000,
                    });
                });
                await _8_8_1.test.step('Navigate to Cross Component Test Project', async () => {
                    await modelerHomePage.clickMessageBanner();
                    await modelerHomePage.clickCrossComponentProjectFolder();
                });
                await _8_8_1.test.step('Add an IDP application to the project', async () => {
                    await modelerHomePage.clickDiagramTypeDropdown();
                    await modelerHomePage.clickIdpApplicationTemplateOption();
                });
                await _8_8_1.test.step('Creates a document extraction template', async () => {
                    await modelerHomePage.enterIdpApplicationName(idpApplicationName);
                    await modelerHomePage.selectCluster(clusterName);
                    await modelerHomePage.clickCreateIdpApplicationButton();
                    await idpCreatePage.clickCreateIdpTemplateButton();
                    await idpCreatePage.fillIdpExtractionTemplateName(`${idpDocumentExtractionTemplateName}`);
                    await idpCreatePage.fillIdpDescriptionField(`${idpDocumentExtractionTemplateName} description`);
                    await idpCreatePage.selectProvider(params.provider);
                    await idpCreatePage.clickCreateIdpExtractationTemplateButton();
                });
                await _8_8_1.test.step('Upload a file for extraction', async () => {
                    await idpCreatePage.clickUnstructuredUploadFilesButton();
                    await (0, fileUpload_1.uploadFile)(page, params.uploadFiles);
                    await idpCreatePage.clickExtractButton();
                });
                await _8_8_1.test.step('Add a field for extraction and extract the data', async () => {
                    await idpCreatePage.fillExtractFieldName('bank_account_number');
                    await idpCreatePage.fillPromptField();
                    await idpCreatePage.clickAddExtractionFieldButton();
                    await idpCreatePage.selectExtractionModelOption(params.model);
                    await idpCreatePage.clickExtractDocumentButton();
                });
                await _8_8_1.test.step('Save extracted data as a test case', async () => {
                    await idpCreatePage.clickSaveAsTestCaseButton();
                    await idpCreatePage.clickCloseToastNotification();
                });
                await _8_8_1.test.step('Validate extraction', async () => {
                    await idpCreatePage.clickValidateExtractionTab();
                    await idpCreatePage.clickExpandAllRowsButton();
                    await idpCreatePage.assertPassTestCaseResultsRow();
                });
                await _8_8_1.test.step('Publish the IDP application to project', async () => {
                    await idpCreatePage.clickPublishButton();
                    await idpCreatePage.clickPublishToProjecOption();
                    await idpCreatePage.fillVersionFieldName(idpApplicationName);
                    await idpCreatePage.fillVersionFieldDescription(idpApplicationName);
                    await idpCreatePage.clickPublishButtonModal();
                    await idpCreatePage.assertIdpSuccessPublishNotification();
                });
            });
        }
    });
    _8_8_1.test.describe('Structured Extraction', () => {
        for (const params of structuredTestCases) {
            (0, _8_8_1.test)(`${params.provider} structured - User creates an idp application with a document extraction template`, async ({ page, idpCreatePage, modelerHomePage, homePage, appsPage, clusterPage, clusterSecretsPage, }) => {
                _8_8_1.test.slow();
                idpApplicationName = await (0, randomName_1.randomNameAgregator)(`${params.provider} structured` + ' - IDP_Application_Name');
                idpDocumentExtractionTemplateName = await (0, randomName_1.randomNameAgregator)(`${params.provider} structured` + ' - IDP_Extraction_Template_Name');
                await _8_8_1.test.step('Navigate to Connectors Secrets', async () => {
                    await homePage.clickClusters();
                    await clusterPage.clickClusterLink(clusterName);
                    await clusterPage.clickConnectorSecretsTab(clusterName);
                    await clusterSecretsPage.assertSecretsOrCreate(clusterName, connectorSecrets_1.allConnectorSecrets[params.provider]);
                });
                await _8_8_1.test.step('Navigate to Web Modeler', async () => {
                    await (0, test_1.expect)(homePage.camundaComponentsButton).toBeVisible({
                        timeout: 120000,
                    });
                    await appsPage.clickCamundaApps();
                    await appsPage.clickModeler();
                    await (0, test_1.expect)(modelerHomePage.modelerPageBanner).toBeVisible({
                        timeout: 120000,
                    });
                });
                await _8_8_1.test.step('Navigate to Cross Component Test Project', async () => {
                    await modelerHomePage.clickMessageBanner();
                    await modelerHomePage.clickCrossComponentProjectFolder();
                });
                await _8_8_1.test.step('Add an IDP application to the project', async () => {
                    await modelerHomePage.clickDiagramTypeDropdown();
                    await modelerHomePage.clickIdpApplicationTemplateOption();
                });
                await _8_8_1.test.step('Creates a document extraction template', async () => {
                    await modelerHomePage.enterIdpApplicationName(idpApplicationName);
                    await modelerHomePage.selectCluster(clusterName);
                    await modelerHomePage.clickCreateIdpApplicationButton();
                    await idpCreatePage.clickCreateIdpTemplateButton();
                    await idpCreatePage.selectStructuredDataExtractionMethod();
                    await idpCreatePage.fillIdpExtractionTemplateName(`${idpDocumentExtractionTemplateName}`);
                    await idpCreatePage.fillIdpDescriptionField(`${idpDocumentExtractionTemplateName} - description`);
                    await idpCreatePage.selectProvider(params.provider);
                    await idpCreatePage.clickCreateIdpExtractationTemplateButton();
                });
                await _8_8_1.test.step('Upload a file for extraction', async () => {
                    await idpCreatePage.clickStructuredUploadFilesButton();
                    await (0, fileUpload_1.uploadFile)(page, params.uploadFiles);
                });
                await _8_8_1.test.step('Select all the fields extracted rows', async () => {
                    await idpCreatePage.selectAllExtractionFields();
                });
                await _8_8_1.test.step('Switch to the tables tab', async () => {
                    await idpCreatePage.clickTablesTab();
                });
                await _8_8_1.test.step('Select all the extracted tables', async () => {
                    await idpCreatePage.selectAllExtractedTables();
                });
                await _8_8_1.test.step('Confirm selection of fields and tables', async () => {
                    await idpCreatePage.assertFieldsAndTablesSelection();
                });
                await _8_8_1.test.step('Click on the Test Template tab', async () => {
                    await idpCreatePage.clickTestTemplateTab();
                });
                await _8_8_1.test.step('Upload multiples files for testing the extraction', async () => {
                    await idpCreatePage.clickStructuredUploadFilesButton();
                    await (0, fileUpload_1.uploadFile)(page, params.extractionFiles);
                    await idpCreatePage.clickTestExtractionTemplateButton();
                });
                await _8_8_1.test.step('Test Summary Results and Details Results sections are visible ', async () => {
                    await idpCreatePage.extractionResultsAreVisible();
                });
                await _8_8_1.test.step('Publish the IDP application to project', async () => {
                    await idpCreatePage.clickPublishTemplateButton();
                    await idpCreatePage.clickPublishToProjecOption();
                    await idpCreatePage.fillVersionFieldName(idpApplicationName);
                    await idpCreatePage.fillVersionFieldDescription(idpApplicationName);
                    await idpCreatePage.clickPublishTemplateToProjectButton();
                    await idpCreatePage.assertStructuredTemplateSuccessNotification();
                });
            });
        }
    });
});
