"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorMarketplacePage = void 0;
class ConnectorMarketplacePage {
    page;
    searchForConnectorTextbox;
    downloadToProjectButton;
    snackbar;
    closeButton;
    replaceResourceButton;
    addToProjectButton;
    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.replaceResourceButton = page.getByRole('button', {
            name: 'Replace resource',
        });
        // Camunda Hub's ImportModal renders one of three primary buttons depending
        // on state. Source: camunda-hub
        // frontend/apps/hub/src/components/ImportModal/get-action-buttons-props.js
        //   - "Add to project"   for a brand-new resource (showPublish)
        //   - "Replace resource" when a duplicate exists and replace is allowed
        //   - "Save as copy"     when a duplicate exists but replace is NOT
        //                        allowed (e.g. an existing project template
        //                        conflict — user must save the import under a
        //                        new ID to keep both)
        this.addToProjectButton = page.getByRole('button', {
            name: 'Add to project',
        });
        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 "Download to project", the Hub ImportModal opens. Per
        // camunda-hub
        // frontend/apps/hub/src/components/ImportModal/get-action-buttons-props.js
        // the primary button is one of:
        //   - "Add to project"   fresh import — we want to confirm
        //   - "Replace resource" duplicate exists, replace allowed
        //   - "Save as copy"     duplicate exists, replace NOT allowed
        //
        // Strategy:
        //   - If "Add to project" is visible, click it: the connector wasn't
        //     in the project, this commits the fresh import.
        //   - Otherwise the connector is already in the project (conflict).
        //     Cancel the modal: Save as copy would create a duplicate under a
        //     different name (and the test then can't find "Worldwide Public
        //     Holiday"), and Replace risks other tests' state. The connector
        //     already exists — clickPublicHolidayConnectorOption just needs
        //     to find it in the change-element popup, which it does via a
        //     state reset.
        if (await this.addToProjectButton
            .isVisible({ timeout: 20000 })
            .catch(() => false)) {
            await this.addToProjectButton.click({ timeout: 30000 });
            return;
        }
        if (await this.cancelButton.isVisible({ timeout: 5000 }).catch(() => false)) {
            await this.cancelButton.click({ timeout: 30000 });
        }
        // If neither button is visible the import dialog never opened; nothing
        // to dismiss and downstream state-reset will recover.
    }
}
exports.ConnectorMarketplacePage = ConnectorMarketplacePage;
