"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkdownContent = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_markdown_1 = __importDefault(require("react-markdown"));
const remark_gfm_1 = __importDefault(require("remark-gfm"));
const react_core_1 = require("@patternfly/react-core");
const CodeBlockMessage_1 = __importDefault(require("../Message/CodeBlockMessage/CodeBlockMessage"));
const TextMessage_1 = __importDefault(require("../Message/TextMessage/TextMessage"));
const ListItemMessage_1 = __importDefault(require("../Message/ListMessage/ListItemMessage"));
const UnorderedListMessage_1 = __importDefault(require("../Message/ListMessage/UnorderedListMessage"));
const OrderedListMessage_1 = __importDefault(require("../Message/ListMessage/OrderedListMessage"));
const TableMessage_1 = __importDefault(require("../Message/TableMessage/TableMessage"));
const TrMessage_1 = __importDefault(require("../Message/TableMessage/TrMessage"));
const TdMessage_1 = __importDefault(require("../Message/TableMessage/TdMessage"));
const TbodyMessage_1 = __importDefault(require("../Message/TableMessage/TbodyMessage"));
const TheadMessage_1 = __importDefault(require("../Message/TableMessage/TheadMessage"));
const ThMessage_1 = __importDefault(require("../Message/TableMessage/ThMessage"));
const ImageMessage_1 = __importDefault(require("../Message/ImageMessage/ImageMessage"));
const rehype_unwrap_images_1 = __importDefault(require("rehype-unwrap-images"));
const rehype_external_links_1 = __importDefault(require("rehype-external-links"));
const rehype_sanitize_1 = __importDefault(require("rehype-sanitize"));
const rehype_highlight_1 = __importDefault(require("rehype-highlight"));
require("highlight.js/styles/vs2015.css");
const LinkMessage_1 = __importDefault(require("../Message/LinkMessage/LinkMessage"));
const rehypeMoveImagesOutOfParagraphs_1 = require("../Message/Plugins/rehypeMoveImagesOutOfParagraphs");
const SuperscriptMessage_1 = __importDefault(require("../Message/SuperscriptMessage/SuperscriptMessage"));
const react_styles_1 = require("@patternfly/react-styles");
const MarkdownContent = ({ content, isMarkdownDisabled, codeBlockProps, tableProps, openLinkInNewTab = true, additionalRehypePlugins = [], additionalRemarkPlugins = [], linkProps, reactMarkdownProps, remarkGfmProps, hasNoImages = false, isPrimary, textComponent, shouldRetainStyles }) => {
    let rehypePlugins = [rehype_unwrap_images_1.default, rehypeMoveImagesOutOfParagraphs_1.rehypeMoveImagesOutOfParagraphs, rehype_highlight_1.default];
    if (openLinkInNewTab) {
        rehypePlugins = rehypePlugins.concat([[rehype_external_links_1.default, { target: '_blank' }, rehype_sanitize_1.default]]);
    }
    if (additionalRehypePlugins) {
        rehypePlugins.push(...additionalRehypePlugins);
    }
    const disallowedElements = hasNoImages ? ['img'] : [];
    if (reactMarkdownProps && reactMarkdownProps.disallowedElements) {
        disallowedElements.push(...reactMarkdownProps.disallowedElements);
    }
    if (isMarkdownDisabled) {
        if (textComponent) {
            return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: textComponent });
        }
        return ((0, jsx_runtime_1.jsx)(TextMessage_1.default, { component: react_core_1.ContentVariants.p, isPrimary: isPrimary, children: content }));
    }
    return ((0, jsx_runtime_1.jsx)(react_markdown_1.default, Object.assign({ components: {
            section: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return ((0, jsx_runtime_1.jsx)("section", Object.assign({}, rest, { className: (0, react_styles_1.css)('pf-chatbot__message-text', shouldRetainStyles && 'pf-m-markdown', rest === null || rest === void 0 ? void 0 : rest.className) })));
            },
            p: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return ((0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.p }, rest, { isPrimary: isPrimary })));
            },
            code: (_a) => {
                var { children } = _a, props = __rest(_a, ["children"]);
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, codeProps = __rest(props, ["node"]);
                return ((0, jsx_runtime_1.jsx)(CodeBlockMessage_1.default, Object.assign({}, codeProps, codeBlockProps, { isPrimary: isPrimary, shouldRetainStyles: shouldRetainStyles, children: children })));
            },
            h1: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h1 }, rest));
            },
            h2: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h2 }, rest));
            },
            h3: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h3 }, rest));
            },
            h4: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h4 }, rest));
            },
            h5: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h5 }, rest));
            },
            h6: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.h6 }, rest));
            },
            blockquote: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return ((0, jsx_runtime_1.jsx)(TextMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: react_core_1.ContentVariants.blockquote }, rest)));
            },
            ul: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(UnorderedListMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles }, rest));
            },
            ol: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(OrderedListMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles }, rest));
            },
            li: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(ListItemMessage_1.default, Object.assign({}, rest));
            },
            // table requires node attribute for calculating headers for mobile breakpoint
            table: (props) => ((0, jsx_runtime_1.jsx)(TableMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles }, props, tableProps, { isPrimary: isPrimary }))),
            tbody: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TbodyMessage_1.default, Object.assign({}, rest));
            },
            thead: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TheadMessage_1.default, Object.assign({}, rest));
            },
            tr: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(TrMessage_1.default, Object.assign({}, rest));
            },
            td: (props) => {
                // Conflicts with Td type
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node, width } = props, rest = __rest(props, ["node", "width"]);
                return (0, jsx_runtime_1.jsx)(TdMessage_1.default, Object.assign({}, rest));
            },
            th: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(ThMessage_1.default, Object.assign({}, rest));
            },
            img: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(ImageMessage_1.default, Object.assign({}, rest));
            },
            a: (props) => {
                // node is just the details of the document structure - not needed
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (
                // some a types conflict with ButtonProps, but it's ok because we are using an a tag
                // there are too many to handle manually
                (0, jsx_runtime_1.jsx)(LinkMessage_1.default, Object.assign({ shouldRetainStyles: shouldRetainStyles }, rest, linkProps, { children: props.children })));
            },
            // used for footnotes
            sup: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (0, jsx_runtime_1.jsx)(SuperscriptMessage_1.default, Object.assign({}, rest));
            }
        }, remarkPlugins: [[remark_gfm_1.default, Object.assign({}, remarkGfmProps)], ...additionalRemarkPlugins], rehypePlugins: rehypePlugins }, reactMarkdownProps, { remarkRehypeOptions: Object.assign({ 
            // removes sr-only class from footnote labels applied by default
            footnoteLabelProperties: { className: [''] } }, reactMarkdownProps === null || reactMarkdownProps === void 0 ? void 0 : reactMarkdownProps.remarkRehypeOptions), disallowedElements: disallowedElements, children: content })));
};
exports.MarkdownContent = MarkdownContent;
exports.default = exports.MarkdownContent;
