"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectorSettingsPage = void 0;
const test_1 = require("@playwright/test");
class ConnectorSettingsPage {
    page;
    urlInput;
    resultExpressionInput;
    authenticationTypeDropdown;
    usernameInput;
    passwordInput;
    methodTypeDropdown;
    resultVariableInput;
    bearerTokenInput;
    authenticationTab;
    outputMappingTab;
    httpEndpointTab;
    correlationTab;
    inputTab;
    webhookConfigurationTab;
    constructor(page) {
        this.page = page;
        this.urlInput = page.getByLabel('URL');
        this.resultExpressionInput = page.getByLabel('Result expression');
        this.authenticationTypeDropdown = page.getByLabel('Type');
        this.usernameInput = page.getByLabel('Username');
        this.passwordInput = page.getByLabel('Password', { exact: true });
        this.methodTypeDropdown = page.getByLabel('Method');
        this.resultVariableInput = page.getByLabel('Result variable');
        this.bearerTokenInput = page.getByLabel('Bearer token');
        this.authenticationTab = page
            .locator('div')
            .filter({ hasText: /^Authentication$/ })
            .first();
        this.outputMappingTab = page
            .locator('div')
            .filter({ hasText: /^Output mapping$/ })
            .first();
        this.httpEndpointTab = page
            .locator('div')
            .filter({ hasText: /^HTTP endpoint$/ })
            .first();
        this.correlationTab = page
            .locator('div')
            .filter({ hasText: /^Correlation$/ })
            .first();
        this.inputTab = page
            .locator('div')
            .filter({ hasText: /^Input$/ })
            .first();
        this.webhookConfigurationTab = page
            .locator('div')
            .filter({ hasText: /^Webhook configuration$/ })
            .first();
    }
    async clickUrlInput() {
        try {
            await this.urlInput.click({ timeout: 60000 });
        }
        catch {
            await this.httpEndpointTab.click();
            await this.urlInput.click({ timeout: 60000 });
        }
    }
    async fillUrlInput(url) {
        await this.urlInput.fill(url);
        await (0, test_1.expect)(this.page.getByText('URL must not be empty.')).not.toBeVisible({ timeout: 30000 });
    }
    async clickResultExpressionInput() {
        try {
            await this.resultExpressionInput.click();
        }
        catch {
            await this.clickOutputMappingTab();
            await this.resultExpressionInput.click();
        }
    }
    async fillResultExpressionInput(resultExpression) {
        await this.resultExpressionInput.fill(resultExpression);
    }
    async selectAuthenticationType(authentication) {
        await this.authenticationTypeDropdown.selectOption(authentication);
    }
    async clickUsernameInput() {
        await this.usernameInput.click({ timeout: 30000 });
    }
    async fillUsernameInput(username) {
        await this.usernameInput.fill(username, { timeout: 30000 });
        await (0, test_1.expect)(this.page.getByText('Username must not be empty.')).not.toBeVisible({ timeout: 30000 });
    }
    async clickPasswordInput() {
        await this.passwordInput.click();
    }
    async fillPasswordInput(password) {
        await this.passwordInput.fill(password);
        await (0, test_1.expect)(this.page.getByText('Password must not be empty.')).not.toBeVisible({ timeout: 30000 });
    }
    async selectMethodType(method) {
        await this.methodTypeDropdown.selectOption(method, { timeout: 60000 });
    }
    async clickResultVariableInput() {
        await this.resultVariableInput.click();
    }
    async fillResultVariableInput(variable) {
        await this.resultVariableInput.fill(variable);
    }
    async clickBearerTokenInput() {
        await this.bearerTokenInput.click({ timeout: 30000 });
    }
    async fillBearerTokenInput(token) {
        await this.bearerTokenInput.fill(token);
        await (0, test_1.expect)(this.page.getByText('Bearer token must not be empty.')).not.toBeVisible({ timeout: 30000 });
    }
    // Connector settings tabs can take several seconds to render on slower clusters
    // after the element type is changed. Each clickXxxTab method retries with a
    // reload-free wait so a single slow render doesn't cause a hard timeout failure.
    async clickTabWithRetry(tab, tabName, maxAttempts = 3) {
        let lastError;
        for (let attempt = 0; attempt < maxAttempts; attempt++) {
            try {
                await (0, test_1.expect)(tab).toBeVisible({ timeout: 30000 });
                await tab.click({ timeout: 30000 });
                return;
            }
            catch (err) {
                lastError = err;
                console.warn(`clickTabWithRetry: "${tabName}" attempt ${attempt + 1}/${maxAttempts} failed: ${err instanceof Error ? err.message : err}`);
            }
        }
        throw lastError instanceof Error
            ? lastError
            : new Error(`Tab "${tabName}" not clickable after ${maxAttempts} attempts`);
    }
    async clickAuthenticationTab() {
        await this.clickTabWithRetry(this.authenticationTab, 'Authentication');
    }
    async clickOutputMappingTab() {
        await this.clickTabWithRetry(this.outputMappingTab, 'Output mapping');
    }
    async clickHTTPEndpointTab() {
        await this.clickTabWithRetry(this.httpEndpointTab, 'HTTP endpoint');
    }
    async clickCorrelationTab() {
        await this.clickTabWithRetry(this.correlationTab, 'Correlation');
    }
    async clickInputTab() {
        await this.clickTabWithRetry(this.inputTab, 'Input');
    }
    async clickWebhookConfigurationTab() {
        await this.clickTabWithRetry(this.webhookConfigurationTab, 'Webhook configuration');
    }
}
exports.ConnectorSettingsPage = ConnectorSettingsPage;
