"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
const sleep_1 = require("../../utils/sleep");
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    replaceResourceButton;
    cancelButton;
    marketplaceErrorLocator;
    constructor(page) {
        this.page = page;
        this.searchForConnectorTextbox = page.getByPlaceholder('Search for a connector');
        this.downloadToProjectButton = page
            .getByRole('button', { name: 'Download to project' })
            .first();
        this.cancelButton = page.getByRole('button', { name: 'Cancel' });
        this.replaceResourceButton = page.getByRole('button', {
            name: 'Replace resource',
        });
        this.marketplaceErrorLocator = page.getByText('an error occurred');
    }
    async clickSearchForConnectorTextbox() {
        await this.searchForConnectorTextbox.click({ timeout: 60000 });
    }
    async fillSearchForConnectorTextbox(connectorName) {
        await this.searchForConnectorTextbox.fill(connectorName, { timeout: 60000 });
        // Wait for search debounce + client-side filtering to complete
        await (0, sleep_1.sleep)(5000);
    }
    async clickDownloadToProjectButton() {
        await this.downloadToProjectButton.click({ timeout: 60000 });
    }
    async clickCancelButton() {
        await this.cancelButton.click({ timeout: 30000 });
    }
    async clickReplaceResourceButton() {
        await this.replaceResourceButton.click({ timeout: 30000 });
    }
    async searchAndDownloadConnector(connectorName) {
        // The marketplace backend is an external service that intermittently
        // returns "an error occurred, please try again later" instead of the
        // connector listing. Re-issue the search with an increasing backoff so a
        // transient blip self-heals rather than failing the whole flow. A
        // sustained outage still surfaces (the connector-dependent step downstream
        // fails) rather than being masked.
        const maxAttempts = 4;
        for (let attempt = 1; attempt <= maxAttempts; attempt++) {
            await this.searchForConnectorTextbox.click({ timeout: 60000 });
            await this.searchForConnectorTextbox.fill(connectorName, {
                timeout: 60000,
            });
            // Wait for search debounce + client-side filtering to complete.
            await (0, sleep_1.sleep)(5000);
            const downloadOrError = this.downloadToProjectButton.or(this.marketplaceErrorLocator);
            await downloadOrError
                .first()
                .waitFor({ state: 'visible', timeout: 30000 })
                .catch(() => undefined);
            if (await this.downloadToProjectButton.isVisible()) {
                await this.downloadConnectorToProject();
                return;
            }
            // Marketplace returned an error (or nothing rendered). Clear the query
            // and retry after a backoff so the next search re-hits the backend.
            if (attempt < maxAttempts) {
                await this.searchForConnectorTextbox.fill('');
                await (0, sleep_1.sleep)(2000 * attempt);
            }
        }
        // Retries exhausted — close the dialog so the caller can proceed; the
        // connector-dependent step downstream will surface a clear failure.
        await this.page.keyboard.press('Escape');
    }
    async downloadConnectorToProject() {
        // Race the download button against a marketplace API error so we don't
        // block the full 60 s timeout when the marketplace backend is unavailable.
        const downloadOrError = this.downloadToProjectButton.or(this.marketplaceErrorLocator);
        try {
            await downloadOrError.first().waitFor({ state: 'visible', timeout: 60000 });
        }
        catch {
            // Neither appeared — close the dialog and let the caller proceed.
            await this.page.keyboard.press('Escape');
            return;
        }
        if (await this.marketplaceErrorLocator.isVisible()) {
            // Marketplace API error — dismiss the dialog and return gracefully so
            // the caller can still attempt clickWebhookStartEventConnectorOption().
            await this.page.keyboard.press('Escape');
            return;
        }
        await this.downloadToProjectButton.click({ timeout: 10000 });
        try {
            await Promise.race([
                this.replaceResourceButton.click({ timeout: 20000 }),
                this.cancelButton.click({ timeout: 20000 }),
            ]);
        }
        catch (e) {
            return;
        }
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
