"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.UNHIDE_ALL_ISSUES = exports.UNHIDE_THIS_ISSUE = exports.HIDE_THIS_ISSUE = exports.HIDE_ISSUES_MENU = exports.RESOURCES_LABEL = exports.REPORT_ONLY_STATUS = exports.BLOCKED_STATUS = exports.SOURCES_LINK = exports.ELEMENTS_PANEL_SELECTOR = exports.ELEMENT_REVEAL_ICON = exports.AFFECTED_ELEMENT_ICON = exports.ISSUE_TITLE = exports.ISSUE = exports.KIND_CHECKBOX = exports.CATEGORY_CHECKBOX = exports.CATEGORY_NAME = exports.KIND = exports.CATEGORY = void 0;
exports.getHideIssuesMenu = getHideIssuesMenu;
exports.navigateToIssuesTab = navigateToIssuesTab;
exports.getUnhideAllIssuesBtn = getUnhideAllIssuesBtn;
exports.getHideIssuesMenuItem = getHideIssuesMenuItem;
exports.getUnhideIssuesMenuItem = getUnhideIssuesMenuItem;
exports.getHiddenIssuesRow = getHiddenIssuesRow;
exports.getHiddenIssuesRowBody = getHiddenIssuesRowBody;
exports.assertCategoryName = assertCategoryName;
exports.assertIssueTitle = assertIssueTitle;
exports.getIssueByTitle = getIssueByTitle;
exports.getAndExpandSpecificIssueByTitle = getAndExpandSpecificIssueByTitle;
exports.getIssueHeaderByTitle = getIssueHeaderByTitle;
exports.assertStatus = assertStatus;
exports.expandCategory = expandCategory;
exports.expandKind = expandKind;
exports.expandIssue = expandIssue;
exports.getResourcesElement = getResourcesElement;
exports.ensureResourceSectionIsExpanded = ensureResourceSectionIsExpanded;
exports.waitForTableFromResourceSection = waitForTableFromResourceSection;
exports.waitForTableFromResourceSectionContents = waitForTableFromResourceSectionContents;
exports.getGroupByCategoryChecked = getGroupByCategoryChecked;
exports.getGroupByKindChecked = getGroupByKindChecked;
exports.revealNodeInElementsPanel = revealNodeInElementsPanel;
exports.revealViolatingSourcesLines = revealViolatingSourcesLines;
exports.toggleGroupByCategory = toggleGroupByCategory;
exports.toggleGroupByKind = toggleGroupByKind;
const chai_1 = require("chai");
const helper_js_1 = require("../../shared/helper.js");
const settings_helpers_js_1 = require("./settings-helpers.js");
exports.CATEGORY = '.issue-category:not(.hidden-issues)';
exports.KIND = '.issue-kind';
exports.CATEGORY_NAME = '.issue-category .title';
exports.CATEGORY_CHECKBOX = '[title^="Group displayed issues under"]';
exports.KIND_CHECKBOX = '[title^="Group displayed issues as"]';
exports.ISSUE = '.issue:not(.hidden-issue)';
exports.ISSUE_TITLE = '.issue .title';
exports.AFFECTED_ELEMENT_ICON = '.affected-resource-csp-info-node';
exports.ELEMENT_REVEAL_ICON = '.element-reveal-icon';
exports.ELEMENTS_PANEL_SELECTOR = '.panel[aria-label="elements"]';
exports.SOURCES_LINK = '.affected-source-location > button';
exports.BLOCKED_STATUS = '.affected-resource-blocked-status';
exports.REPORT_ONLY_STATUS = '.affected-resource-report-only-status';
exports.RESOURCES_LABEL = '.affected-resource-label';
exports.HIDE_ISSUES_MENU = 'devtools-hide-issues-menu';
exports.HIDE_THIS_ISSUE = 'Hide issues like this';
exports.UNHIDE_THIS_ISSUE = 'Unhide issues like this';
exports.UNHIDE_ALL_ISSUES = '.unhide-all-issues-button';
async function getHideIssuesMenu(root, devToolsPage) {
    return await devToolsPage.waitFor(exports.HIDE_ISSUES_MENU, root);
}
async function navigateToIssuesTab(devToolsPage) {
    await (0, settings_helpers_js_1.openPanelViaMoreTools)('Issues', devToolsPage);
}
async function getUnhideAllIssuesBtn(devToolsPage) {
    const btn = await devToolsPage.waitFor(exports.UNHIDE_ALL_ISSUES);
    return btn;
}
async function getHideIssuesMenuItem(devToolsPage) {
    const menuItem = await devToolsPage.waitFor(`[aria-label="${exports.HIDE_THIS_ISSUE}"]`);
    if (menuItem) {
        return menuItem;
    }
    return null;
}
async function getUnhideIssuesMenuItem(devToolsPage) {
    return await devToolsPage.waitFor(`[aria-label="${exports.UNHIDE_THIS_ISSUE}"]`);
}
async function getHiddenIssuesRow(devToolsPage) {
    return await devToolsPage.waitFor('.hidden-issues');
}
async function getHiddenIssuesRowBody(devToolsPage) {
    return await devToolsPage.waitFor('.hidden-issues-body');
}
async function assertCategoryName(categoryName, devToolsPage) {
    const categoryNameElement = await devToolsPage.waitFor(exports.CATEGORY_NAME);
    const selectedCategoryName = await categoryNameElement.evaluate(node => node.textContent);
    chai_1.assert.strictEqual(selectedCategoryName, categoryName);
}
async function assertIssueTitle(issueMessage, devToolsPage) {
    const issueMessageElement = await devToolsPage.waitFor(exports.ISSUE_TITLE);
    const selectedIssueMessage = await issueMessageElement.evaluate(node => node.textContent);
    chai_1.assert.strictEqual(selectedIssueMessage, issueMessage);
}
async function getIssueByTitleElement(issueMessageElement) {
    const header = await issueMessageElement.evaluateHandle(el => el.parentElement);
    if (header) {
        const headerClassList = await header.evaluate(el => el.classList.toString());
        chai_1.assert.include(headerClassList, 'header');
        const issue = (await header.evaluateHandle(el => el.parentElement?.nextSibling)).asElement();
        if (issue) {
            return issue;
        }
    }
    return undefined;
}
/** Only works if there is just a single issue. **/
async function getIssueByTitle(issueMessage, devToolsPage) {
    const issueMessageElement = await devToolsPage.waitFor(exports.ISSUE_TITLE);
    const selectedIssueMessage = await issueMessageElement.evaluate(node => node.textContent);
    chai_1.assert.strictEqual(selectedIssueMessage, issueMessage);
    return await getIssueByTitleElement(issueMessageElement);
}
/** Works also if there are multiple issues. **/
async function getAndExpandSpecificIssueByTitle(issueMessage, devToolsPage) {
    const issueMessageElement = await devToolsPage.waitForFunction(async () => {
        const issueElements = await devToolsPage.$$(exports.ISSUE_TITLE);
        for (const issueElement of issueElements) {
            const message = await issueElement.evaluate((issueElement) => issueElement.textContent);
            if (message === issueMessage) {
                return issueElement;
            }
        }
        return undefined;
    });
    await issueMessageElement.click();
    await devToolsPage.waitFor('.message');
    return await getIssueByTitleElement(issueMessageElement);
}
async function getIssueHeaderByTitle(issueMessage, devToolsPage) {
    const issueMessageElement = await devToolsPage.waitForFunction(async () => await devToolsPage.$textContent(issueMessage, undefined) ?? undefined);
    const header = await issueMessageElement.evaluateHandle(el => el.parentElement);
    if (header) {
        return header;
    }
    return undefined;
}
async function assertStatus(status, devToolsPage) {
    const classStatus = status === 'blocked' ? exports.BLOCKED_STATUS : exports.REPORT_ONLY_STATUS;
    const issueMessageElement = await devToolsPage.waitFor(classStatus);
    const selectedIssueMessage = await issueMessageElement.evaluate(node => node.textContent);
    chai_1.assert.strictEqual(selectedIssueMessage, status);
}
async function expandCategory(devToolsPage) {
    const categoryElement = await devToolsPage.waitFor(exports.CATEGORY);
    const isCategoryExpanded = await categoryElement.evaluate(node => node.classList.contains('expanded'));
    if (!isCategoryExpanded) {
        await devToolsPage.click(exports.CATEGORY);
    }
    await devToolsPage.waitFor(exports.ISSUE);
}
async function expandKind(classSelector, devToolsPage) {
    const kindElement = await devToolsPage.waitFor(`${exports.KIND}${classSelector}`);
    const isKindExpanded = await kindElement.evaluate(node => node.classList.contains('expanded'));
    if (!isKindExpanded) {
        await kindElement.click();
    }
    await devToolsPage.waitFor(exports.ISSUE);
}
async function expandIssue(devToolsPage) {
    if (await getGroupByCategoryChecked(devToolsPage)) {
        await expandCategory(devToolsPage);
    }
    const issue = await devToolsPage.waitFor(exports.ISSUE);
    await issue.click();
    await devToolsPage.waitFor('.message');
}
async function getResourcesElement(resourceName, issueElement, className, devToolsPage) {
    return await devToolsPage.waitForFunction(async () => {
        const elements = await devToolsPage.$$(className ?? exports.RESOURCES_LABEL, issueElement);
        for (const el of elements) {
            const text = await el.evaluate(el => el.textContent);
            if (text?.includes(resourceName)) {
                const content = await el.evaluateHandle(el => el.parentElement?.nextSibling);
                return { label: el, content: content };
            }
        }
        return undefined;
    });
}
async function ensureResourceSectionIsExpanded(section, devToolsPage) {
    await section.label.evaluate(el => {
        el.scrollIntoView();
    });
    const isExpanded = await devToolsPage.hasClass(section.content, 'expanded');
    if (!isExpanded) {
        await section.label.click();
    }
    await devToolsPage.waitForClass(section.content, 'expanded');
}
async function extractTableFromResourceSection(resourceContentElement) {
    const table = await resourceContentElement.$('.affected-resource-list');
    if (table) {
        return await table.evaluate(table => {
            const rows = [];
            for (const tableRow of table.childNodes) {
                const row = [];
                for (const cell of tableRow.childNodes) {
                    const requestLinkIcon = cell instanceof HTMLElement && cell.querySelector('devtools-request-link-icon');
                    if (requestLinkIcon) {
                        const label = requestLinkIcon.shadowRoot?.querySelector('[aria-label="Shortened URL"]');
                        row.push(label?.textContent || '');
                    }
                    else {
                        row.push(cell.textContent || '');
                    }
                }
                rows.push(row);
            }
            return rows;
        });
    }
    return undefined;
}
async function waitForTableFromResourceSection(resourceContentElement, predicate, devToolsPage) {
    return await devToolsPage.waitForFunction(async () => {
        const table = await extractTableFromResourceSection(resourceContentElement);
        if (!table || predicate(table) !== true) {
            return undefined;
        }
        return table;
    });
}
function waitForTableFromResourceSectionContents(resourceContentElement, expected, devToolsPage) {
    return waitForTableFromResourceSection(resourceContentElement, table => (0, helper_js_1.matchStringTable)(table, expected) === true ? true : undefined, devToolsPage);
}
async function getGroupByCategoryChecked(devToolsPage) {
    const categoryCheckbox = await devToolsPage.waitFor(exports.CATEGORY_CHECKBOX);
    return await categoryCheckbox.evaluate(node => node.checked);
}
async function getGroupByKindChecked(devToolsPage) {
    const categoryCheckbox = await devToolsPage.waitFor(exports.KIND_CHECKBOX);
    return await categoryCheckbox.evaluate(node => node.checked);
}
async function revealNodeInElementsPanel(devToolsPage) {
    const revealIcon = await devToolsPage.waitFor(exports.ELEMENT_REVEAL_ICON);
    await revealIcon.click();
}
async function revealViolatingSourcesLines(devToolsPage) {
    const sourcesLink = await devToolsPage.waitFor(exports.SOURCES_LINK);
    await sourcesLink.click();
}
async function toggleGroupByCategory(devToolsPage) {
    const wasChecked = await getGroupByCategoryChecked(devToolsPage);
    const categoryCheckbox = await devToolsPage.waitFor(exports.CATEGORY_CHECKBOX);
    // Invoke `click()` directly on the checkbox to toggle while hidden.
    await categoryCheckbox.evaluate(checkbox => checkbox.click());
    if (wasChecked) {
        await devToolsPage.waitFor(exports.ISSUE);
    }
    else {
        await devToolsPage.waitFor(exports.CATEGORY);
    }
}
async function toggleGroupByKind(devToolsPage) {
    const wasChecked = await getGroupByKindChecked(devToolsPage);
    const kindCheckbox = await devToolsPage.waitFor(exports.KIND_CHECKBOX);
    // Invoke `click()` directly on the checkbox to toggle while hidden.
    await kindCheckbox.evaluate(checkbox => checkbox.click());
    if (wasChecked) {
        await devToolsPage.waitFor(exports.ISSUE);
    }
    else {
        await devToolsPage.waitFor(exports.KIND);
    }
}
//# sourceMappingURL=issues-helpers.js.map