"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    closeButton;
    replaceResourceButton;
    addToProjectButton;
    saveAsCopyButton;
    importModal;
    constructor(page) {
        this.page = page;
        this.searchForConnectorTextbox = page.getByPlaceholder('Search for a connector');
        this.downloadToProjectButton = page
            .getByRole('button', { name: 'Download to project' })
            .first();
        this.snackbar = page.locator('[data-test="snackbar"]');
        this.closeButton = page.getByRole('button', { name: 'Close' });
        this.replaceResourceButton = page.getByRole('button', {
            name: 'Replace resource',
        });
        this.addToProjectButton = page.getByRole('button', {
            name: 'Add to project',
        });
        this.saveAsCopyButton = page.getByRole('button', { name: 'Save as copy' });
        this.importModal = page.locator('[data-test="import-modal"]');
    }
    async clickSearchForConnectorTextbox() {
        await this.searchForConnectorTextbox.click({ timeout: 60000 });
    }
    async fillSearchForConnectorTextbox(connectorName) {
        await this.searchForConnectorTextbox.fill(connectorName, { timeout: 60000 });
    }
    async clickDownloadToProjectButton() {
        await this.downloadToProjectButton.click({ timeout: 60000 });
    }
    async clickSnackbar() {
        await this.snackbar.click({ timeout: 120000 });
    }
    async clickCloseButton() {
        await this.closeButton.click({ timeout: 60000 });
    }
    async clickReplaceResourceButton() {
        await this.replaceResourceButton.click({ timeout: 60000 });
    }
    async downloadConnectorToProject() {
        await this.clickDownloadToProjectButton();
        // "Download to project" opens Web Modeler's "Import resource" modal, which
        // can take ~20s to appear. The import-confirming button depends on whether
        // the connector template already exists in the project: an existing one
        // shows "Save as copy" (the conflict view), a brand-new resource shows
        // "Add to project", and older modeler builds used "Replace resource".
        // Probe each with isVisible() and click the first that is present; never
        // "Cancel", which aborts the import and leaves the connector out of the
        // project, breaking the downstream change-element selection.
        const importConfirmButtons = [
            this.saveAsCopyButton,
            this.addToProjectButton,
            this.replaceResourceButton,
        ];
        let imported = false;
        // Wait for each candidate button with waitFor(), NOT isVisible():
        // isVisible() checks the current state and returns immediately, so it fires
        // before the ~20s-slow modal has rendered and reports every button absent —
        // the import is never confirmed and the modal is left blocking the canvas.
        // The first probe gets a long timeout to absorb the modal's slow
        // appearance; once the modal is up the remaining buttons resolve at once.
        let probeTimeout = 60000;
        for (const button of importConfirmButtons) {
            const buttonVisible = await button
                .waitFor({ state: 'visible', timeout: probeTimeout })
                .then(() => true)
                .catch(() => false);
            if (buttonVisible) {
                await button.click({ timeout: 30000 });
                imported = true;
                break;
            }
            probeTimeout = 5000;
        }
        // After a successful import, unblock the diagram canvas before the caller
        // selects the newly installed connector.
        if (imported) {
            // Wait for the "Import resource" modal to close after confirming — while
            // it is still up its subtree intercepts pointer events on the canvas and
            // any downstream change-element click retries fruitlessly against it.
            await this.importModal
                .waitFor({ state: 'hidden', timeout: 60000 })
                .catch(() => { });
            // The modeler closes the marketplace panel itself once the import
            // finishes, so only close it if it is somehow still open — gated on the
            // marketplace's own search box. A bare getByRole('button', {name:
            // 'Close'}) would otherwise match the properties panel's "Close" (×)
            // button and collapse it, hiding the connector's "Input" tab the caller
            // needs next. (Do not idle-wait on the snackbar here either: it never
            // matches the import toast, so it only stalls ~60s, during which the
            // modeler auto-collapses the properties panel.)
            const marketplaceStillOpen = await this.searchForConnectorTextbox
                .isVisible()
                .catch(() => false);
            if (marketplaceStillOpen &&
                (await this.closeButton.isVisible({ timeout: 5000 }).catch(() => false))) {
                await this.closeButton.click({ timeout: 10000 });
            }
        }
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
