"use strict";
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
Object.defineProperty(exports, "__esModule", { value: true });
exports.toggleIgnoreListing = exports.setIgnoreListPattern = exports.togglePreferenceInSettingsTab = exports.closeSettings = exports.openSettingsTab = void 0;
exports.openPanelViaMoreTools = openPanelViaMoreTools;
async function openPanelViaMoreTools(panelTitle, frontend) {
    await frontend.bringToFront();
    // Head to the triple dot menu.
    await frontend.click('aria/Customize and control DevTools');
    await frontend.waitForFunction(async () => {
        // Open the “More Tools” option.
        await frontend.hover('aria/More tools[role="menuitem"]');
        return await frontend.$(`${panelTitle}[role="menuitem"]`, undefined, 'aria');
    });
    // Click the desired menu item
    await frontend.click(`aria/${panelTitle}[role="menuitem"]`);
    // Wait for the triple dot menu to be collapsed.
    const button = await frontend.waitForAria('Customize and control DevTools');
    await frontend.waitForFunction(async () => {
        const expanded = await button.evaluate(el => el.getAttribute('aria-expanded'));
        return expanded === null;
    });
    // Wait for the corresponding panel to appear.
    await frontend.waitForAria(`${panelTitle} panel[role="tabpanel"]`);
}
const openSettingsTab = async (tabTitle, devToolsPage) => {
    const gearIconSelector = 'devtools-button[aria-label="Settings"]';
    const settingsMenuSelector = `.tabbed-pane-header-tab[aria-label="${tabTitle}"]`;
    const panelSelector = `.view-container[aria-label="${tabTitle} panel"]`;
    // Click on the Settings Gear toolbar icon.
    await devToolsPage.click(gearIconSelector);
    // Click on the Settings tab and wait for the panel to appear.
    await devToolsPage.click(settingsMenuSelector);
    await devToolsPage.waitFor(panelSelector);
};
exports.openSettingsTab = openSettingsTab;
const closeSettings = async (devToolsPage) => {
    await devToolsPage.click('.dialog-close-button');
};
exports.closeSettings = closeSettings;
const togglePreferenceInSettingsTab = async (label, shouldBeChecked, devToolsPage) => {
    await (0, exports.openSettingsTab)('Preferences', devToolsPage);
    const selector = `[aria-label="${label}"]`;
    await devToolsPage.scrollElementIntoView(selector);
    const preference = await devToolsPage.waitFor(selector);
    const value = await preference.evaluate(checkbox => checkbox.checked);
    if (value !== shouldBeChecked) {
        await devToolsPage.clickElement(preference);
        await devToolsPage.waitForFunction(async () => {
            const newValue = await preference.evaluate(checkbox => checkbox.checked);
            return newValue !== value;
        });
    }
    await (0, exports.closeSettings)(devToolsPage);
};
exports.togglePreferenceInSettingsTab = togglePreferenceInSettingsTab;
const setIgnoreListPattern = async (pattern, devToolsPage) => {
    await (0, exports.openSettingsTab)('Ignore list', devToolsPage);
    await devToolsPage.click('[aria-label="Add a regular expression rule for the script\'s URL"]');
    const textBox = await devToolsPage.waitFor('[aria-label="Add a regular expression rule for the script\'s URL"]');
    await devToolsPage.clickElement(textBox);
    await textBox.type(pattern);
    await textBox.type('\n');
    await devToolsPage.waitFor(`[title="Ignore scripts whose names match '${pattern}'"]`);
    await (0, exports.closeSettings)(devToolsPage);
};
exports.setIgnoreListPattern = setIgnoreListPattern;
const toggleIgnoreListing = async (enable, devToolsPage) => {
    await (0, exports.openSettingsTab)('Ignore list', devToolsPage);
    await devToolsPage.setCheckBox('[title="Ignore listing"]', enable);
    await (0, exports.closeSettings)(devToolsPage);
};
exports.toggleIgnoreListing = toggleIgnoreListing;
//# sourceMappingURL=settings-helpers.js.map