"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClusterSecretsPage = void 0;
const test_1 = require("@playwright/test");
class ClusterSecretsPage {
    page;
    createNewSecretButton;
    keyInput;
    valueInput;
    createButton;
    optionsButton;
    deleteConnectorSecretButton;
    deleteConnectorSecretSubButton;
    dialog;
    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.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 optionButton = options[0];
                if (await optionButton.isVisible()) {
                    await optionButton.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 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 createNewSecret(cluster, key, value) {
        await this.clickCreateNewSecretButton(cluster);
        await this.clickKeyInput();
        await this.fillKeyInput(key);
        await this.clickValueInput();
        await this.fillValueInput(value);
        await this.clickCreateButton();
        await (0, test_1.expect)(this.page.getByText('Creating...')).not.toBeVisible({
            timeout: 60000,
        });
    }
    async clickCreateNewSecretButton(clusterName) {
        try {
            await (0, test_1.expect)(this.createNewSecretButton).toBeVisible({
                timeout: 30000,
            });
            await this.createNewSecretButton.click({ timeout: 30000 });
        }
        catch (error) {
            await this.clickClusterBanner();
            await this.clickCluster(clusterName);
            await this.clusterSecretTab.click({ timeout: 30000 });
            await this.createNewSecretButton.click({ timeout: 30000 });
        }
    }
    async clickKeyInput() {
        await this.keyInput.click();
    }
    async fillKeyInput(key) {
        await this.keyInput.fill(key);
    }
    async clickValueInput() {
        await this.valueInput.click();
    }
    async fillValueInput(value) {
        await this.valueInput.fill(value);
    }
    async clickCreateButton() {
        await this.createButton.click();
    }
}
exports.ClusterSecretsPage = ClusterSecretsPage;
