"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    closeButton;
    replaceResourceButton;
    addToProjectButton;
    saveAsCopyButton;
    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' });
    }
    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;
        // The first probe gets a long timeout to absorb the modal's slow (~20s)
        // appearance; once the modal is up the remaining buttons resolve at once.
        let probeTimeout = 60000;
        for (const button of importConfirmButtons) {
            if (await button.isVisible({ timeout: probeTimeout }).catch(() => false)) {
                await button.click({ timeout: 30000 });
                imported = true;
                break;
            }
            probeTimeout = 5000;
        }
        // After a successful import, wait for the snackbar (confirms the connector
        // template was received by the modeler) then close the marketplace panel so
        // the diagram canvas is unblocked before the caller tries to select the
        // newly installed connector.
        if (imported) {
            await this.snackbar
                .waitFor({ state: 'visible', timeout: 60000 })
                .catch(() => { });
            if (await this.closeButton.isVisible({ timeout: 5000 }).catch(() => false)) {
                await this.closeButton.click({ timeout: 10000 });
            }
        }
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
