"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OptimizeSharePage = void 0;
const test_1 = require("@playwright/test");
class OptimizeSharePage {
    page;
    shareButton;
    enableSharingToggle;
    sharingToggleLabel;
    shareLinkInput;
    downloadButton;
    constructor(page) {
        this.page = page;
        this.shareButton = page
            .locator('.share-button button')
            .or(page.getByRole('button', { name: /share/i }))
            .first();
        this.enableSharingToggle = page
            .locator('#sharingToggle')
            .or(page.locator('.ShareEntity .cds--toggle__switch'))
            .or(page.getByRole('switch'))
            .first();
        // Carbon's toggle overlays the <button role="switch"> with a <label> whose
        // text/appearance intercepts pointer events, so clicking the switch button
        // directly times out. The label (`for="sharingToggle"`) is the real click
        // target that flips the toggle.
        this.sharingToggleLabel = page
            .locator('label[for="sharingToggle"]')
            .or(page.locator('.ShareEntity .cds--toggle__label'))
            .or(page.locator('.ShareEntity .cds--toggle__appearance'))
            .first();
        this.shareLinkInput = page
            .locator('#sharingLinkText')
            .or(page.locator('.ShareEntity input[type="text"]'))
            .or(page.locator('input[value*="/share/"]'))
            .first();
        // Optimize renders the CSV export as a single icon button (DownloadButton)
        // on raw-data reports, not a menu. Its accessible name comes from the
        // `report.downloadCSV` label ("Download CSV").
        this.downloadButton = page
            .getByRole('button', { name: /download|csv/i })
            .or(page.locator('.DownloadButton button'))
            .or(page.locator('a.DownloadButton'))
            .first();
    }
    async openEntityById(baseUrl, entityType, entityId) {
        const url = `${baseUrl}/#/${entityType}/${entityId}`;
        await this.page.goto(url, {
            waitUntil: 'domcontentloaded',
            timeout: 60000,
        });
        // Optimize is a HashRouter single-page app. When the browser is already on
        // an Optimize page (e.g. the home dashboard), a hash-only navigation does
        // not reliably trigger the router to render the target entity view. Force a
        // full document reload at the hash route so the entity (and its
        // always-present Share control) renders from scratch. Use waitFor (which
        // actually polls) rather than isVisible (a one-shot check), reloading only
        // when the Share control still hasn't rendered.
        for (let attempt = 0; attempt < 3; attempt++) {
            const shareVisible = await this.shareButton
                .waitFor({ state: 'visible', timeout: 10000 })
                .then(() => true)
                .catch(() => false);
            if (shareVisible) {
                return;
            }
            await this.page.reload({ waitUntil: 'domcontentloaded' });
        }
        await (0, test_1.expect)(this.shareButton).toBeVisible({ timeout: 30000 });
    }
    async enableSharing() {
        await this.shareButton.click({ timeout: 30000 });
        await (0, test_1.expect)(this.enableSharingToggle).toBeVisible({ timeout: 30000 });
        await this.sharingToggleLabel.click({ timeout: 30000 });
    }
    async assertShareLinkVisible() {
        await (0, test_1.expect)(this.shareLinkInput).toBeVisible({ timeout: 30000 });
        // Enabling sharing triggers an async backend call that generates the public
        // share URL; the input is briefly empty until it resolves. toHaveValue
        // auto-retries until the value is populated, avoiding a race with a
        // one-shot value read.
        await (0, test_1.expect)(this.shareLinkInput).toHaveValue(/share/i, { timeout: 30000 });
    }
    async disableSharing() {
        await (0, test_1.expect)(this.enableSharingToggle).toBeVisible({ timeout: 30000 });
        await this.sharingToggleLabel.click({ timeout: 30000 });
    }
    async assertDownloadOptionAvailable() {
        await (0, test_1.expect)(this.downloadButton).toBeVisible({ timeout: 30000 });
    }
}
exports.OptimizeSharePage = OptimizeSharePage;
