"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;
    deployErrorMessage;
    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.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.textField = page.locator('button[data-field-type="textfield"]');
        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!' });
        this.deployErrorMessage = this.dialog.getByText('The form failed to deploy');
    }
    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"') {
        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();
        await (0, test_1.expect)(this.page.getByText('Successfully generated smart form.')).toBeVisible({ timeout: 300000 });
        await (0, sleep_1.sleep)(60000);
    }
    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 = 120000;
        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) {
        // After a 8.7->8.8 upgrade, orchestration-cluster authorization commands
        // propagate across partitions for several minutes; deploy is rejected with
        // FORBIDDEN until the deployer's grant lands. In migration runs, reload and
        // retry through that propagation window instead of failing on the transient
        // rejection. Non-migration runs keep the original single-attempt behavior.
        const maxAttempts = process.env.IS_MIGRATION === 'true' ? 15 : 1;
        for (let attempt = 1; attempt <= maxAttempts; attempt++) {
            await this.clickDeployButton();
            await (0, test_1.expect)(this.dialog).toBeVisible({
                timeout: 30000,
            });
            await this.selectCluster(clusterName);
            await this.clickDeploySubButton();
            if (attempt === maxAttempts) {
                await (0, test_1.expect)(this.deploySuccessNotification).toBeVisible({
                    timeout: 30000,
                });
                return;
            }
            const deployed = await this.deploySuccessNotification
                .waitFor({ state: 'visible', timeout: 30000 })
                .then(() => true)
                .catch(() => false);
            if (deployed) {
                return;
            }
            await this.page.reload();
        }
    }
}
exports.FormJsPage = FormJsPage;
