"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    replaceResourceButton;
    addToProjectButton;
    cancelButton;
    closeButton;
    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.replaceResourceButton = page.getByRole('button', {
            name: 'Replace resource',
        });
        this.addToProjectButton = page.getByRole('button', {
            name: 'Add to project',
        });
        this.cancelButton = page.getByRole('button', { name: 'Cancel' });
        this.closeButton = page.getByRole('button', { name: 'Close' });
    }
    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 clickCloseButton() {
        await this.closeButton.click({ timeout: 60000 });
    }
    async clickReplaceResourceButton() {
        await this.replaceResourceButton.click({ timeout: 60000 });
    }
    async downloadConnectorToProject() {
        await this.clickDownloadToProjectButton();
        let imported = false;
        // A fresh download shows "Add to project"; an already-present resource
        // shows "Replace resource". Pick whichever the ImportModal rendered —
        // the old code only handled "Replace resource", so a first-time download
        // (the SaaS nightly always runs in a fresh project) was never confirmed
        // and the connector never reached the diagram.
        if (await this.addToProjectButton
            .isVisible({ timeout: 20000 })
            .catch(() => false)) {
            await this.addToProjectButton.click({ timeout: 10000 });
            imported = true;
        }
        else if (await this.replaceResourceButton
            .isVisible({ timeout: 5000 })
            .catch(() => false)) {
            await this.replaceResourceButton.click({ timeout: 10000 });
            imported = true;
        }
        else if (await this.cancelButton.isVisible({ timeout: 3000 }).catch(() => false)) {
            await this.cancelButton.click({ timeout: 10000 });
        }
        // 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;
