"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Markdown = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _message = require("../message");
var _expandableContent = require("../expandable-content");
var _markdownUtils = require("./markdown-utils");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/** Renders markdown content as HTML with support for GFM, math, and syntax highlighting.
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const Markdown = ({
  children,
  className,
  maxHeight
}) => {
  const [html, setHtml] = (0, _react.useState)('');
  const [error, setError] = (0, _react.useState)('');
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  const clearPrevChildrenState = () => {
    if (error) {
      setError('');
    }
    if (html) {
      setHtml('');
    }
  };
  (0, _react.useEffect)(() => {
    clearPrevChildrenState();
    Promise.all([Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "unified" */'unified'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "remark-gfm" */'remark-gfm'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "remark-parse" */'remark-parse'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "remark-rehype" */'remark-rehype'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "rehype-sanitize" */'rehype-sanitize'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "rehype-stringify" */'rehype-stringify'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "rehype-highlight" */'rehype-highlight'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "rehype-katex" */'rehype-katex'))), Promise.resolve().then(() => _interopRequireWildcard(require(/* webpackChunkName: "remark-math" */'remark-math')))]).then(([{
      unified
    }, {
      default: remarkGfm
    }, {
      default: remarkParse
    }, {
      default: remarkRehype
    }, {
      default: rehypeSanitize
    }, {
      default: rehypeStringify
    }, {
      default: rehypeHighlight
    }, {
      default: rehypeKatex
    }, {
      default: remarkMath
    }]) => {
      unified().use(remarkParse, {
        fragment: true
      })
      // Remark plugins:
      .use(remarkMath).use(remarkGfm).use(remarkRehype)
      // Rehype plugins:
      .use(rehypeSanitize).use(rehypeKatex, {
        output: 'html'
      }).use(rehypeHighlight).use(_markdownUtils.datarobotCSSClasses).use(rehypeStringify).process(children).then(result => {
        setHtml(result.toString());
      });
    }).catch(err => {
      setError(err);
    });
  }, [children]);
  if (error) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_message.Message, {
      type: _message.MESSAGE_TYPES.ERROR,
      children: t('Something went wrong processing response from the model. Try reloading the page.')
    });
  }
  if (!html) {
    return null;
  }
  const content = /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)('markdown', className),
    dangerouslySetInnerHTML: {
      __html: String(html)
    }
  });
  if (maxHeight !== undefined) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_expandableContent.ExpandableContent, {
      maxHeight: maxHeight,
      children: content
    });
  }
  return content;
};
exports.Markdown = Markdown;