import { defaultRunIf } from "./models";
/**
 * Finds and remove the instance of `abr-index` in the document
 */
export const clean = () => { var _a; return (_a = document.querySelectorAll("abr-index")) === null || _a === void 0 ? void 0 : _a.forEach((e) => e.remove()); };
export const getViolationList = ({ violations, incomplete }, allowlist) => [...violations, ...incomplete]
    .filter(({ id }) => !allowlist.find((item) => item === id))
    .map((violation) => (Object.assign(Object.assign({}, violation), { nodes: violation.nodes.filter((node) => node.html !== "<abr-index></abr-index>") })))
    .filter((violation) => !!violation.nodes.length);
const replaceTag = (tag) => {
    const tagsToReplace = {
        "&": "&amp;",
        "<": "&lt;",
        ">": "&gt;",
    };
    return tagsToReplace[tag] || tag;
};
export const encodeHTMLTags = (str) => str.replace(/[&<>]/g, replaceTag);
const generateNodeHTML = (node) => `<p>${encodeHTMLTags(node.failureSummary)}</p><code>${encodeHTMLTags(node.html)}</code>`;
export const generateViolationHTML = (violation) => {
    var _a;
    return `<abr-status-dot status="${violation.impact}" slot="start"></abr-status-dot><span slot="heading">${encodeHTMLTags(violation.help)} (<code>${violation.id}</code>)</span><blockquote>${encodeHTMLTags(violation.description)}</blockquote>
          ${(_a = violation.nodes) === null || _a === void 0 ? void 0 : _a.map(generateNodeHTML).join(" ")}
          <a href="${violation.helpUrl}" target="_blank">Read more...</a>`;
};
export const computeRunIfCondition = (config) => {
    if (config) {
        if (config.runIf) {
            return config.runIf;
        }
    }
    return defaultRunIf;
};
export const highlightViolation = (node) => {
    var _a;
    const elements = document.querySelectorAll((_a = node.target) === null || _a === void 0 ? void 0 : _a[0]);
    elements.forEach((element) => {
        element.style.outline = "2px dashed orange";
        element.style.outlineOffset = "2px";
    });
};
export const unHighlightViolation = (node) => {
    var _a;
    const elements = document.querySelectorAll((_a = node.target) === null || _a === void 0 ? void 0 : _a[0]);
    elements.forEach((element) => {
        element.style.outline = "";
        element.style.outlineOffset = "";
    });
};
