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;
};
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { ContentVariants } from '@patternfly/react-core';
import CodeBlockMessage from '../Message/CodeBlockMessage/CodeBlockMessage';
import TextMessage from '../Message/TextMessage/TextMessage';
import ListItemMessage from '../Message/ListMessage/ListItemMessage';
import UnorderedListMessage from '../Message/ListMessage/UnorderedListMessage';
import OrderedListMessage from '../Message/ListMessage/OrderedListMessage';
import TableMessage from '../Message/TableMessage/TableMessage';
import TrMessage from '../Message/TableMessage/TrMessage';
import TdMessage from '../Message/TableMessage/TdMessage';
import TbodyMessage from '../Message/TableMessage/TbodyMessage';
import TheadMessage from '../Message/TableMessage/TheadMessage';
import ThMessage from '../Message/TableMessage/ThMessage';
import ImageMessage from '../Message/ImageMessage/ImageMessage';
import rehypeUnwrapImages from 'rehype-unwrap-images';
import rehypeExternalLinks from 'rehype-external-links';
import rehypeSanitize from 'rehype-sanitize';
import rehypeHighlight from 'rehype-highlight';
import 'highlight.js/styles/vs2015.css';
import LinkMessage from '../Message/LinkMessage/LinkMessage';
import { rehypeMoveImagesOutOfParagraphs } from '../Message/Plugins/rehypeMoveImagesOutOfParagraphs';
import SuperscriptMessage from '../Message/SuperscriptMessage/SuperscriptMessage';
import { css } from '@patternfly/react-styles';
export const MarkdownContent = ({ content, isMarkdownDisabled, codeBlockProps, tableProps, openLinkInNewTab = true, additionalRehypePlugins = [], additionalRemarkPlugins = [], linkProps, reactMarkdownProps, remarkGfmProps, hasNoImages = false, isPrimary, textComponent, shouldRetainStyles }) => {
    let rehypePlugins = [rehypeUnwrapImages, rehypeMoveImagesOutOfParagraphs, rehypeHighlight];
    if (openLinkInNewTab) {
        rehypePlugins = rehypePlugins.concat([[rehypeExternalLinks, { target: '_blank' }, rehypeSanitize]]);
    }
    if (additionalRehypePlugins) {
        rehypePlugins.push(...additionalRehypePlugins);
    }
    const disallowedElements = hasNoImages ? ['img'] : [];
    if (reactMarkdownProps && reactMarkdownProps.disallowedElements) {
        disallowedElements.push(...reactMarkdownProps.disallowedElements);
    }
    if (isMarkdownDisabled) {
        if (textComponent) {
            return _jsx(_Fragment, { children: textComponent });
        }
        return (_jsx(TextMessage, { component: ContentVariants.p, isPrimary: isPrimary, children: content }));
    }
    return (_jsx(Markdown, Object.assign({ components: {
            section: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (_jsx("section", Object.assign({}, rest, { className: 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 (_jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: 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 (_jsx(CodeBlockMessage, 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 _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h1 }, rest));
            },
            h2: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h2 }, rest));
            },
            h3: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h3 }, rest));
            },
            h4: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h4 }, rest));
            },
            h5: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h5 }, rest));
            },
            h6: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.h6 }, rest));
            },
            blockquote: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return (_jsx(TextMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles, component: ContentVariants.blockquote }, rest)));
            },
            ul: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(UnorderedListMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles }, rest));
            },
            ol: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(OrderedListMessage, Object.assign({ shouldRetainStyles: shouldRetainStyles }, rest));
            },
            li: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(ListItemMessage, Object.assign({}, rest));
            },
            // table requires node attribute for calculating headers for mobile breakpoint
            table: (props) => (_jsx(TableMessage, 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 _jsx(TbodyMessage, Object.assign({}, rest));
            },
            thead: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TheadMessage, Object.assign({}, rest));
            },
            tr: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(TrMessage, 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 _jsx(TdMessage, Object.assign({}, rest));
            },
            th: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(ThMessage, Object.assign({}, rest));
            },
            img: (props) => {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                const { node } = props, rest = __rest(props, ["node"]);
                return _jsx(ImageMessage, 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
                _jsx(LinkMessage, 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 _jsx(SuperscriptMessage, Object.assign({}, rest));
            }
        }, remarkPlugins: [[remarkGfm, 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 })));
};
export default MarkdownContent;
