"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;
    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',
        });
    }
    async clickSearchForConnectorTextbox() {
        await this.searchForConnectorTextbox.click({ timeout: 60000 });
        await (0, sleep_1.sleep)(2000);
    }
    async fillSearchForConnectorTextbox(connectorName) {
        await this.searchForConnectorTextbox.fill(connectorName, { timeout: 60000 });
        await (0, sleep_1.sleep)(2000);
    }
    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 downloadConnectorToProject() {
        await this.clickDownloadToProjectButton();
        try {
            await Promise.race([
                this.replaceResourceButton.click({ timeout: 20000 }),
                this.cancelButton.click({ timeout: 20000 }),
            ]);
        }
        catch (e) {
            return;
        }
    }
    // The marketplace search backend can transiently return HTTP 504
    // ("upstream request timeout"), which leaves the modal in an error or
    // "We couldn't find a match" state with no "Download to project" button.
    // Re-issue the search (clearing the field first) until the button appears,
    // so a single slow search response doesn't fail the whole flow.
    async searchAndDownloadConnector(connectorName, maxRetries = 4) {
        for (let attempt = 0; attempt < maxRetries; attempt++) {
            await this.clickSearchForConnectorTextbox();
            await this.searchForConnectorTextbox.fill('', { timeout: 60000 });
            await this.fillSearchForConnectorTextbox(connectorName);
            try {
                await this.downloadToProjectButton.waitFor({
                    state: 'visible',
                    timeout: 30000,
                });
                await this.downloadConnectorToProject();
                return;
            }
            catch (e) {
                if (attempt === maxRetries - 1) {
                    throw new Error(`Connector "${connectorName}" did not appear in the marketplace ` +
                        `after ${maxRetries} search attempts; the marketplace search ` +
                        `API may be returning errors (e.g. HTTP 504).`);
                }
                await (0, sleep_1.sleep)(5000);
            }
        }
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
