"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    replaceResourceButton;
    cancelButton;
    marketplaceErrorMessage;
    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.marketplaceErrorMessage = page.getByText('an error occurred, please try again later');
    }
    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 waitForConnectorSearchResults() {
        const MAX_RETRIES = 3;
        for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
            await (0, test_1.expect)(this.downloadToProjectButton.or(this.marketplaceErrorMessage)).toBeVisible({ timeout: 60000 });
            if (await this.downloadToProjectButton.isVisible()) {
                return;
            }
            console.log(`Marketplace search attempt ${attempt} of ${MAX_RETRIES} returned an error. Retrying search...`);
            if (attempt < MAX_RETRIES) {
                const searchText = await this.searchForConnectorTextbox.inputValue();
                await this.searchForConnectorTextbox.clear();
                await (0, sleep_1.sleep)(2000);
                await this.searchForConnectorTextbox.fill(searchText);
                await (0, sleep_1.sleep)(2000);
            }
        }
        throw new Error('Marketplace connector search failed after retries due to persistent error');
    }
    async downloadConnectorToProject() {
        await this.clickDownloadToProjectButton();
        try {
            await Promise.race([
                this.replaceResourceButton.click({ timeout: 20000 }),
                this.cancelButton.click({ timeout: 20000 }),
            ]);
        }
        catch (e) {
            return;
        }
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
