"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    closeButton;
    addToProjectButton;
    replaceResourceButton;
    saveAsCopyButton;
    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.addToProjectButton = page.getByRole('button', {
            name: 'Add to project',
        });
        this.replaceResourceButton = page.getByRole('button', {
            name: 'Replace resource',
        });
        this.saveAsCopyButton = page.getByRole('button', { name: 'Save as copy' });
        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();
        // After clicking "Download to project", Hub opens an ImportModal whose
        // primary button depends on project state: "Add to project" (new template),
        // "Replace resource" (same template exists), or "Save as copy" (conflict).
        // Race all three click calls — Playwright's click() waits for actionability,
        // so whichever button Hub actually renders will win the race and be clicked.
        // If none appear within 20 s (e.g. the modal never opened), the race rejects
        // and the catch path handles any residual Cancel button.
        let imported = false;
        try {
            await Promise.race([
                this.addToProjectButton.click({ timeout: 20000 }),
                this.replaceResourceButton.click({ timeout: 20000 }),
                this.saveAsCopyButton.click({ timeout: 20000 }),
            ]);
            imported = true;
        }
        catch (e) {
            if (await this.cancelButton.isVisible({ timeout: 3000 }).catch(() => false)) {
                await this.cancelButton.click({ timeout: 10000 });
            }
        }
        // Wait for the snackbar that confirms the template was received, then close
        // the marketplace dialog so the change-element popup is unblocked before the
        // caller selects 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;
