"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormJsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class FormJsPage {
    page;
    aiFormGeneratorButton;
    formRequestInput;
    generateFormButton;
    formEditor;
    filePicker;
    textField;
    documentPreview;
    keyInput;
    uploadMultipleFilesToggle;
    documentReferenceInput;
    generalPanel;
    textFieldInForm;
    deployButton;
    deploySubButton;
    dialog;
    deploySuccessNotification;
    constructor(page) {
        this.page = page;
        this.aiFormGeneratorButton = page.getByRole('button', {
            name: 'AI Form Generator',
        });
        this.formRequestInput = page.getByPlaceholder('A form for an american standard loan request');
        this.generateFormButton = page.getByRole('button', { name: 'Generate form' });
        this.formEditor = page.getByLabel('Form Definition');
        this.documentPreview = page.locator('button[data-field-type="documentPreview"]');
        this.filePicker = page.locator('button[data-field-type="filepicker"]');
        this.textField = page.locator('button[data-field-type="textfield"]');
        this.keyInput = page.getByRole('textbox', { name: 'key' });
        this.uploadMultipleFilesToggle = page.locator('div[data-entry-id="multiple"] .bio-properties-panel-toggle-switch__switcher');
        this.documentReferenceInput = page.getByRole('textbox', {
            name: 'document reference',
        });
        this.generalPanel = page.locator('[data-group-id="group-general"]').first();
        this.textFieldInForm = page
            .getByLabel('Form Definition')
            .getByText('Text field');
        this.deployButton = page
            .locator('[data-test="action-bar"]')
            .getByRole('button', { name: 'Deploy', exact: true });
        this.dialog = page
            .locator('.cds--modal-container[role="dialog"]')
            .filter({ hasText: 'Deploy form' });
        this.deploySubButton = this.dialog.getByRole('button', {
            name: 'Deploy',
            exact: true,
        });
        this.deploySuccessNotification = page
            .locator('.cds--toast-notification--success')
            .filter({ hasText: 'Form deployed!' });
    }
    async generateAIForm(request = 'Create a form with the following fields: 1. A text field with the label "Full Name" 2. A number with the label "Count" 3. A date input with the label "Date of birth"4. A Checkbox with the label "Agree"') {
        // The "Successfully generated smart form." toast is delivered over a Pusher
        // websocket. A transient websocket/network drop during generation means the
        // toast never renders even though generation finished, leaving the old
        // single 5-minute wait stuck until the test timed out. Re-attempt on a
        // reloaded editor (fresh websocket) if the toast doesn't arrive.
        const maxAttempts = 2;
        for (let attempt = 1; attempt <= maxAttempts; attempt++) {
            await (0, test_1.expect)(this.aiFormGeneratorButton).toBeVisible({
                timeout: 90000,
            });
            await this.clickAIFormGeneratorButton();
            await this.clickFormRequestInput();
            await this.fillFormRequestInput(request);
            await this.clickGenerateFormButton();
            await (0, test_1.expect)(this.page.getByText('Generating...'))
                .toBeVisible({ timeout: 60000 })
                .catch(() => { });
            try {
                await (0, test_1.expect)(this.page.getByText('Successfully generated smart form.')).toBeVisible({ timeout: 180000 });
                await (0, sleep_1.sleep)(60000);
                return;
            }
            catch (error) {
                if (attempt === maxAttempts) {
                    throw error;
                }
                await this.page.reload();
            }
        }
    }
    async clickAIFormGeneratorButton() {
        await this.aiFormGeneratorButton.click();
    }
    async clickFormRequestInput() {
        await this.formRequestInput.click();
    }
    async fillFormRequestInput(formRequest) {
        await this.formRequestInput.fill(formRequest);
    }
    async clickGenerateFormButton() {
        await this.generateFormButton.click();
    }
    async dragAndDrop(source, target) {
        await source.waitFor();
        await target.waitFor();
        await source.dragTo(target);
    }
    async fillKeyInput(key) {
        await this.keyInput.fill(key, { timeout: 60000 });
        await (0, test_1.expect)(this.keyInput).toHaveValue(key, { timeout: 20000 });
        await (0, sleep_1.sleep)(1000);
    }
    async enableUploadMultipleFiles() {
        await this.uploadMultipleFilesToggle.click({ timeout: 60000 });
    }
    async filldocumentReferenceInput(key) {
        await this.documentReferenceInput.fill(key, { timeout: 60000 });
    }
    async clickGeneralPropertiesPanel() {
        await this.generalPanel.click({ timeout: 60000 });
    }
    async selectCluster(clusterName) {
        await (0, test_1.expect)(this.dialog).toBeVisible({ timeout: 30000 });
        const healthCheckTimeout = 30000;
        const healthyRegex = new RegExp(`${clusterName}[\\s\\S]*?HealthydevManage`);
        const cluster = this.dialog.locator(`[title="${clusterName}"]`);
        await (0, test_1.expect)(cluster).toBeVisible({ timeout: 30000 });
        await cluster.click();
        await (0, test_1.expect)(this.dialog
            .locator('label')
            .filter({ hasText: clusterName })
            .getByRole('switch')).toBeChecked({ timeout: 30000 });
        await (0, test_1.expect)(this.dialog).toHaveText(healthyRegex, {
            timeout: healthCheckTimeout,
        });
    }
    async clickDeployButton() {
        await this.deployButton.click({ timeout: 60000 });
    }
    async clickDeploySubButton() {
        await this.deploySubButton.click({ timeout: 60000 });
    }
    async deployForm(clusterName) {
        await this.clickDeployButton();
        await (0, test_1.expect)(this.dialog).toBeVisible({
            timeout: 30000,
        });
        await this.selectCluster(clusterName);
        await this.clickDeploySubButton();
        await (0, test_1.expect)(this.deploySuccessNotification).toBeVisible({ timeout: 30000 });
    }
}
exports.FormJsPage = FormJsPage;
