"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterSecretsPage = void 0;
const test_1 = require("@playwright/test");
const sleep_1 = require("../../utils/sleep");
const expectLocatorWithRetry_1 = require("../../utils/assertionHelpers/expectLocatorWithRetry");
class ClusterSecretsPage {
    page;
    createNewSecretButton;
    keyInput;
    valueInput;
    createButton;
    optionsButton;
    deleteConnectorSecretButton;
    deleteConnectorSecretSubButton;
    dialog;
    importButton;
    fromFileButton;
    bulkImportTextArea;
    dialogImportButton;
    secretsSearchBox;
    clusterBanner;
    clusterLink;
    clusterSecretTab;
    confirmDeleteInput;
    constructor(page) {
        this.page = page;
        this.createNewSecretButton = page.getByRole('button', {
            name: 'Create new secret',
        });
        this.keyInput = page.getByRole('dialog').getByLabel('Key');
        this.valueInput = page
            .getByRole('dialog')
            .getByLabel('Value', { exact: true });
        this.createButton = page.getByRole('button', { name: 'Create', exact: true });
        this.optionsButton = page
            .getByRole('cell', { name: 'Options' })
            .getByRole('button');
        this.deleteConnectorSecretButton = page.getByRole('menuitem', {
            name: 'Delete',
            exact: true,
        });
        this.deleteConnectorSecretSubButton = page.getByRole('button', {
            name: 'danger Delete',
        });
        this.dialog = page.getByRole('dialog');
        this.importButton = page.getByRole('button', { name: 'Import', exact: true });
        this.fromFileButton = page.getByText('from File', { exact: true });
        this.bulkImportTextArea = page.locator('[id="bulk-import-content"]');
        this.dialogImportButton = this.page
            .getByRole('dialog')
            .getByRole('button', { name: 'Import', exact: true });
        this.secretsSearchBox = page.getByRole('searchbox', { name: 'Filter table' });
        this.clusterBanner = this.page
            .getByRole('banner')
            .getByRole('link', { name: 'Clusters' });
        this.clusterLink = (name) => page.getByRole('link', { name: name });
        this.clusterSecretTab = this.page.getByRole('tab', {
            name: 'Connector Secrets',
        });
        this.confirmDeleteInput = page.getByRole('textbox', {
            name: 'Type the word DELETE to confirm',
        });
    }
    async deleteConnectorSecretsIfExist() {
        try {
            await (0, test_1.expect)(this.optionsButton.first()).toBeVisible({
                timeout: 10000,
            });
        }
        catch (error) {
            return; //No Connector Secrets found in the list
        }
        try {
            let options = await this.optionsButton.all();
            while (options.length > 0) {
                const operationButton = options[0];
                if (await operationButton.isVisible()) {
                    await operationButton.click({ timeout: 60000 });
                    await this.deleteConnectorSecretButton.click({ timeout: 60000 });
                    await (0, test_1.expect)(this.dialog).toBeVisible({ timeout: 40000 });
                    await this.deleteConnectorSecretSubButton.click();
                    await (0, test_1.expect)(this.page.getByText('Deleting...')).not.toBeVisible({
                        timeout: 60000,
                    });
                }
                const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
                await sleep(3000);
                options = await this.optionsButton.all();
            }
            await (0, test_1.expect)(this.optionsButton).not.toBeVisible({
                timeout: 30000,
            });
        }
        catch (error) {
            console.error('Error during Connector Secrets deletion: ', error);
        }
    }
    async clickCreateNewSecretButton(cluster) {
        try {
            await this.createNewSecretButton.click({ timeout: 60000 });
        }
        catch (error) {
            await this.clickClusterBanner();
            await this.clickCluster(cluster);
            await this.clusterSecretTab.click({ timeout: 30000 });
            await this.createNewSecretButton.click({ timeout: 30000 });
        }
    }
    async clickClusterBanner() {
        await (0, test_1.expect)(this.clusterBanner).toBeVisible({ timeout: 30000 });
        await this.clusterBanner.click({ timeout: 60000 });
    }
    async clickCluster(name) {
        await (0, test_1.expect)(this.clusterLink(name)).toBeVisible({ timeout: 30000 });
        await this.clusterLink(name).click();
    }
    async clickKeyInput() {
        await (0, test_1.expect)(this.keyInput).toBeVisible({ timeout: 60000 });
        await this.keyInput.click({ timeout: 30000 });
    }
    async fillKeyInput(key) {
        await this.keyInput.fill(key, { timeout: 30000 });
    }
    async clickValueInput() {
        await (0, test_1.expect)(this.valueInput).toBeVisible({ timeout: 60000 });
        await this.valueInput.click({ timeout: 30000 });
    }
    async fillValueInput(value) {
        const maxRetries = 3;
        for (let attempt = 1; attempt <= maxRetries; attempt++) {
            try {
                await this.valueInput.fill(value, { timeout: 30000 });
                const inputValue = await this.valueInput.inputValue();
                if (inputValue === value) {
                    return;
                }
                console.log(`Attempt ${attempt}: Value input not filled correctly. Expected: "${value}", Got: "${inputValue}"`);
            }
            catch (error) {
                console.log(`Attempt ${attempt}: Error filling value input:`, error);
            }
            if (attempt < maxRetries) {
                await this.valueInput.clear();
                await this.valueInput.click({ timeout: 30000 });
                await (0, sleep_1.sleep)(1000);
            }
        }
        throw new Error(`Failed to fill value input after ${maxRetries} attempts`);
    }
    async clickCreateButton() {
        await (0, test_1.expect)(this.createButton).toBeVisible({ timeout: 60000 });
        await this.createButton.click({ timeout: 30000 });
    }
    async createNewSecret(key, value, cluster) {
        await this.clickCreateNewSecretButton(cluster);
        await this.clickKeyInput();
        await this.fillKeyInput(key);
        await (0, sleep_1.sleep)(1000);
        await this.clickValueInput();
        await this.fillValueInput(value);
        await (0, sleep_1.sleep)(1000);
        await this.clickCreateButton();
        await (0, test_1.expect)(this.page.getByText('Creating...')).not.toBeVisible({
            timeout: 60000,
        });
        await (0, test_1.expect)(this.createNewSecretButton).toBeVisible({
            timeout: 60000,
        });
    }
    async createSetOfSecrets(cluster, secrets) {
        await this.bulkImportSecrets(secrets);
        await this.assertSecretsOrCreate(cluster, secrets);
    }
    async assertSecretsOrCreate(cluster, secrets) {
        for (const secret of secrets) {
            try {
                await (0, test_1.expect)(this.secretsSearchBox).toBeVisible();
                await this.secretsSearchBox.click();
                await this.secretsSearchBox.fill(secret.name);
                const row = this.page.getByRole('row', {
                    name: new RegExp(`^${secret.name}\\b`, 'i'),
                });
                await (0, test_1.expect)(row).toBeVisible({ timeout: 5000 });
            }
            catch (error) {
                console.error(`Creating secret for ${secret.name}`);
                await this.createNewSecret(secret.name, secret.value, cluster);
            }
        }
    }
    async clickImportButton() {
        await (0, test_1.expect)(this.importButton).toBeVisible({ timeout: 30000 });
        await this.importButton.click({ timeout: 30000 });
    }
    async bulkImportSecrets(secrets) {
        const mapped = secrets
            .map((secret) => `${secret.name}=${secret.value}`)
            .join('\n');
        await this.clickImportButton();
        if (!(await this.dialog.isVisible())) {
            await (0, expectLocatorWithRetry_1.expectLocatorWithRetry)(this.page, this.fromFileButton, {
                postAction: async () => {
                    await this.clickImportButton();
                },
            });
            await this.fromFileButton.click();
        }
        await (0, test_1.expect)(this.dialog).toBeVisible();
        await this.bulkImportTextArea.click();
        await this.bulkImportTextArea.fill(mapped);
        await this.dialogImportButton.click();
        try {
            await (0, test_1.expect)(this.dialog).not.toBeVisible({
                timeout: 60000,
            });
        }
        catch {
            // Import dialog didn't close - press Escape and let assertSecretsOrCreate handle creation
            await this.page.keyboard.press('Escape');
            await (0, test_1.expect)(this.dialog).not.toBeVisible({ timeout: 10000 });
        }
        await (0, test_1.expect)(this.createNewSecretButton).toBeVisible({
            timeout: 60000,
        });
        await this.page.reload();
        await (0, sleep_1.sleep)(5000);
    }
}
exports.ClusterSecretsPage = ClusterSecretsPage;
