"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    closeButton;
    replaceResourceButton;
    addToProjectButton;
    cancelButton;
    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.cancelButton = page.getByRole('button', { name: 'Cancel' });
    }
    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();
        let imported = false;
        try {
            await Promise.race([
                this.addToProjectButton.click({ timeout: 20000 }),
                this.replaceResourceButton.click({ timeout: 20000 }),
            ]);
            imported = true;
        }
        catch (e) {
            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;
