"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const _8_10_1 = require("../../fixtures/8.10");
const _setup_1 = require("../../test-setup.js");
const UtilitiesPage_1 = require("../../pages/8.10/UtilitiesPage");
const users_1 = require("../../utils/users");
const apiHelpers_1 = require("../../utils/apiHelpers");
const randomName_1 = require("../../utils/randomName");
const testUser = (0, users_1.getTestUser)('mainUser');
const clusterName = 'Test Cluster';
const DEFINITION_KEY = 'customer_onboarding_en';
_8_10_1.test.describe.configure({ mode: 'parallel' });
_8_10_1.test.describe('Optimize Sharing & Export UI Tests @optimize-sharing-alerts-export-ui @8.10', () => {
    let baseUrl;
    let optimizeCookie;
    let optimizeBearerToken;
    let collectionId;
    let reportId;
    let csvReportId;
    let dashboardId;
    _8_10_1.test.beforeAll(async ({ browser, request }) => {
        baseUrl = process.env.CAMUNDA_OPTIMIZE_BASE_URL;
        const seedPage = await browser.newPage();
        optimizeCookie = await (0, apiHelpers_1.getOptimizeCoockie)(seedPage);
        await seedPage.close();
        optimizeBearerToken = await (0, apiHelpers_1.authSaasAPI)(process.env.OPTIMIZE_API_TOKEN_AUDIENCE);
        collectionId = await (0, apiHelpers_1.createCollection)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Sharing UI Collection'),
            optimizeCookie,
        });
        await (0, apiHelpers_1.updateCollectionScope)(request, {
            optimizeCookie,
            collectionId,
            data: [
                {
                    definitionKey: DEFINITION_KEY,
                    definitionType: 'process',
                    tenants: ['<default>'],
                },
            ],
        });
        // Build the report directly (rather than via the createSingleProcessReport
        // helper, which sends an empty `data: {}` and nests the definition/view in
        // `configuration`, so the fields below would be silently dropped).
        const reportName = await (0, randomName_1.randomNameAgregator)('Sharing UI Report');
        const reportResponse = await (0, apiHelpers_1.retryOn500)(() => request.post(`${baseUrl}/api/report/process/single`, {
            headers: {
                Cookie: optimizeCookie,
                'Content-Type': 'application/json',
            },
            data: {
                collectionId,
                name: reportName,
                data: {
                    definitions: [
                        {
                            identifier: 'definition_1',
                            key: DEFINITION_KEY,
                            versions: ['all'],
                            tenantIds: ['<default>'],
                        },
                    ],
                    filter: [],
                    view: { entity: 'processInstance', properties: ['frequency'] },
                    groupBy: { type: 'none', value: null },
                    distributedBy: { type: 'none', value: null },
                    visualization: 'number',
                    configuration: {},
                },
            },
        }));
        (0, test_1.expect)(reportResponse.status()).toBe(200);
        reportId = (await reportResponse.json()).id;
        dashboardId = await (0, apiHelpers_1.createDashboard)(request, {
            name: await (0, randomName_1.randomNameAgregator)('Sharing UI Dashboard'),
            optimizeCookie,
            collectionId,
        });
        // Optimize only renders the CSV download action on raw-data reports
        // (view.properties includes 'rawData'). Seed a dedicated raw-data report
        // for the CSV export assertion. Wrapped in retryOn500 so a transient 5xx
        // here doesn't fail every test in the file, matching the other setup calls.
        const csvReportName = await (0, randomName_1.randomNameAgregator)('Sharing UI CSV Report');
        const csvReportResponse = await (0, apiHelpers_1.retryOn500)(() => request.post(`${baseUrl}/api/report/process/single`, {
            headers: {
                Cookie: optimizeCookie,
                'Content-Type': 'application/json',
            },
            data: {
                collectionId,
                name: csvReportName,
                data: {
                    definitions: [
                        {
                            identifier: 'definition_1',
                            key: DEFINITION_KEY,
                            versions: ['all'],
                            tenantIds: ['<default>'],
                        },
                    ],
                    filter: [],
                    view: { entity: null, properties: ['rawData'] },
                    groupBy: { type: 'none', value: null },
                    distributedBy: { type: 'none', value: null },
                    visualization: 'table',
                    configuration: {},
                },
            },
        }));
        (0, test_1.expect)(csvReportResponse.status()).toBe(200);
        csvReportId = (await csvReportResponse.json()).id;
    });
    _8_10_1.test.beforeEach(async ({ page, loginPage }, testInfo) => {
        await (0, UtilitiesPage_1.loginWithRetry)(page, loginPage, testUser, (testInfo.workerIndex + 1) * 1000);
    });
    _8_10_1.test.afterEach(async ({ page }, testInfo) => {
        await (0, _setup_1.captureScreenshot)(page, testInfo);
        await (0, _setup_1.captureFailureVideo)(page, testInfo);
    });
    _8_10_1.test.afterAll(async ({ request }) => {
        if (collectionId) {
            await request.delete(`${baseUrl}/api/public/collection/${collectionId}`, {
                headers: { Authorization: optimizeBearerToken },
            });
        }
    });
    (0, _8_10_1.test)('Enable report sharing and expose a public share link', async ({ appsPage, optimizeHomePage, optimizeSharePage, }) => {
        await _8_10_1.test.step('Open Optimize from the Camunda apps launcher', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickOptimize(clusterName);
            await test_1.expect
                .poll(async () => optimizeHomePage.optimizeBanner.isVisible({ timeout: 5000 }), { timeout: 120000 })
                .toBe(true);
        });
        await _8_10_1.test.step('Navigate to the seeded report', async () => {
            await optimizeSharePage.openEntityById(baseUrl, 'report', reportId);
        });
        await _8_10_1.test.step('Enable sharing and assert a public share link is generated', async () => {
            await optimizeSharePage.enableSharing();
            await optimizeSharePage.assertShareLinkVisible();
        });
        await _8_10_1.test.step('Disable sharing to leave the report in its original state', async () => {
            await optimizeSharePage.disableSharing();
        });
    });
    (0, _8_10_1.test)('Report offers a CSV download/export action', async ({ appsPage, optimizeHomePage, optimizeSharePage, }) => {
        await _8_10_1.test.step('Open Optimize from the Camunda apps launcher', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickOptimize(clusterName);
            await test_1.expect
                .poll(async () => optimizeHomePage.optimizeBanner.isVisible({ timeout: 5000 }), { timeout: 120000 })
                .toBe(true);
        });
        await _8_10_1.test.step('Navigate to the seeded raw-data report', async () => {
            await optimizeSharePage.openEntityById(baseUrl, 'report', csvReportId);
        });
        await _8_10_1.test.step('Assert a CSV download/export action is available', async () => {
            await optimizeSharePage.assertDownloadOptionAvailable();
        });
    });
    (0, _8_10_1.test)('Enable dashboard sharing and expose a public share link', async ({ appsPage, optimizeHomePage, optimizeSharePage, }) => {
        await _8_10_1.test.step('Open Optimize from the Camunda apps launcher', async () => {
            await appsPage.clickCamundaApps();
            await appsPage.clickOptimize(clusterName);
            await test_1.expect
                .poll(async () => optimizeHomePage.optimizeBanner.isVisible({ timeout: 5000 }), { timeout: 120000 })
                .toBe(true);
        });
        await _8_10_1.test.step('Navigate to the seeded dashboard', async () => {
            await optimizeSharePage.openEntityById(baseUrl, 'dashboard', dashboardId);
        });
        await _8_10_1.test.step('Enable sharing and assert a public share link is generated', async () => {
            await optimizeSharePage.enableSharing();
            await optimizeSharePage.assertShareLinkVisible();
        });
        await _8_10_1.test.step('Disable sharing to leave the dashboard in its original state', async () => {
            await optimizeSharePage.disableSharing();
        });
    });
});
