"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataFiltersPage = void 0;
const test_1 = require("@playwright/test");
class DataFiltersPage {
    page;
    dataFiltersSection;
    dataFiltersHeading;
    variableFilterToggle;
    includeVariableNamesInput;
    excludeVariableNamesInput;
    includeProcessDefinitionsInput;
    excludeProcessDefinitionsInput;
    saveButton;
    rollingRestartNotice;
    validationError;
    confirmSaveButton;
    constructor(page) {
        this.page = page;
        this.dataFiltersHeading = page
            .getByRole('heading', { name: /data filters/i })
            .or(page.getByText(/data filters/i))
            .first();
        this.dataFiltersSection = page
            .getByRole('region', { name: /data filters/i })
            .or(page
            .locator('section, div')
            .filter({ has: page.getByText(/data filters/i) }))
            .first();
        this.variableFilterToggle = page
            .getByRole('switch', { name: /variable filter/i })
            .or(page.locator('.cds--toggle').filter({ hasText: /variable filter/i }))
            .first();
        this.includeVariableNamesInput = page
            .getByRole('textbox', { name: /include variable names/i })
            .or(page.getByLabel(/include variable names/i))
            .first();
        this.excludeVariableNamesInput = page
            .getByRole('textbox', { name: /exclude variable names/i })
            .or(page.getByLabel(/exclude variable names/i))
            .first();
        this.includeProcessDefinitionsInput = page
            .getByRole('textbox', { name: /include process definitions/i })
            .or(page.getByLabel(/include process definitions/i))
            .first();
        this.excludeProcessDefinitionsInput = page
            .getByRole('textbox', { name: /exclude process definitions/i })
            .or(page.getByLabel(/exclude process definitions/i))
            .first();
        this.saveButton = page
            .getByRole('button', { name: /^save/i })
            .or(page.locator('button.cds--btn--primary').filter({ hasText: /save/i }))
            .first();
        this.confirmSaveButton = page
            .getByRole('dialog')
            .getByRole('button', { name: /save|confirm|continue/i })
            .first();
        this.rollingRestartNotice = page.getByText(/rolling restart/i).first();
        this.validationError = page
            .locator('.cds--form-requirement, [role="alert"]')
            .or(page.getByText(/invalid|required|cannot be empty|maximum length/i))
            .first();
    }
    async assertDataFiltersSectionVisible() {
        await (0, test_1.expect)(this.dataFiltersHeading).toBeVisible({ timeout: 60000 });
    }
    async enableVariableFilter() {
        if (await this.variableFilterToggle.isVisible({ timeout: 30000 })) {
            const checked = await this.variableFilterToggle
                .getAttribute('aria-checked')
                .catch(() => null);
            if (checked !== 'true') {
                await this.variableFilterToggle.click({ timeout: 30000 });
            }
        }
    }
    async disableVariableFilter() {
        if (await this.variableFilterToggle.isVisible({ timeout: 30000 })) {
            const checked = await this.variableFilterToggle
                .getAttribute('aria-checked')
                .catch(() => null);
            if (checked === 'true') {
                await this.variableFilterToggle.click({ timeout: 30000 });
            }
        }
    }
    async addIncludeVariableName(value) {
        await this.includeVariableNamesInput.fill(value, { timeout: 30000 });
        await this.includeVariableNamesInput.press('Enter');
    }
    async addExcludeVariableName(value) {
        await this.excludeVariableNamesInput.fill(value, { timeout: 30000 });
        await this.excludeVariableNamesInput.press('Enter');
    }
    async addIncludeProcessDefinition(value) {
        await this.includeProcessDefinitionsInput.fill(value, { timeout: 30000 });
        await this.includeProcessDefinitionsInput.press('Enter');
    }
    async addExcludeProcessDefinition(value) {
        await this.excludeProcessDefinitionsInput.fill(value, { timeout: 30000 });
        await this.excludeProcessDefinitionsInput.press('Enter');
    }
    async save() {
        await this.saveButton.click({ timeout: 30000 });
        if (await this.confirmSaveButton
            .isVisible({ timeout: 10000 })
            .catch(() => false)) {
            await this.confirmSaveButton.click({ timeout: 30000 });
        }
    }
    async assertRollingRestartNotice() {
        await (0, test_1.expect)(this.rollingRestartNotice).toBeVisible({ timeout: 30000 });
    }
    async assertValidationError() {
        await (0, test_1.expect)(this.validationError).toBeVisible({ timeout: 30000 });
    }
    async assertChipVisible(value) {
        await (0, test_1.expect)(this.page.getByText(value, { exact: false }).first()).toBeVisible({ timeout: 30000 });
    }
    async removeChip(value) {
        const chip = this.page
            .locator('.cds--tag, [class*="chip"], [class*="tag"]')
            .filter({ hasText: value })
            .first();
        const removeButton = chip
            .getByRole('button')
            .or(chip.locator('button, [aria-label*="remove" i], [aria-label*="clear" i]'))
            .first();
        await removeButton.click({ timeout: 30000 });
    }
    async assertControlsDisabled() {
        await (0, test_1.expect)(this.saveButton).toBeDisabled({ timeout: 30000 });
    }
}
exports.DataFiltersPage = DataFiltersPage;
