'use strict';

var commonmark = require('commonmark');
require('../common/theme.js');
require('../common/direction.js');
require('../common/propsValues/control.js');
require('../common/propsValues/breakpoint.js');
require('../common/propsValues/size.js');
require('../common/propsValues/typography.js');
require('../common/propsValues/width.js');
require('../common/propsValues/type.js');
require('../common/propsValues/dateMode.js');
require('../common/propsValues/monthFormat.js');
require('../common/propsValues/position.js');
require('../common/propsValues/layouts.js');
require('../common/propsValues/status.js');
require('../common/propsValues/sentiment.js');
require('../common/propsValues/profileType.js');
require('../common/propsValues/variant.js');
require('../common/propsValues/scroll.js');
var markdownNodeType = require('../common/propsValues/markdownNodeType.js');
require('../common/fileType.js');
require('@transferwise/icons');
require('clsx');
require('react');
require('react-intl');
require('../common/closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var logActionRequired = require('../utilities/logActionRequired.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var commonmark__default = /*#__PURE__*/_interopDefault(commonmark);

const reader = new commonmark__default.default.Parser();
const writer = new commonmark__default.default.HtmlRenderer({
  safe: true
});
const NODE_TYPE_LIST = Object.values(markdownNodeType.MarkdownNodeType);
function Markdown({
  as: Element = 'div',
  allowList,
  blockList,
  config,
  className,
  children
}) {
  if (!children) {
    return null;
  }
  const linkTarget = config?.link?.target ?? '_self';
  const paragraphClass = config?.paragraph?.className ?? '';
  if (allowList != null && blockList != null) {
    logActionRequired.logActionRequired('Markdown supports only one of `allowList` or `blockList` to be used at a time. `blockList` will be ignored.');
  }
  const parser = nodes => {
    const parsed = reader.parse(nodes);
    const toExclude = allowList != null ? NODE_TYPE_LIST.filter(type => !allowList.includes(type)) : blockList;
    return toExclude != null ? stripNodes({
      parsed,
      blockList: toExclude
    }) : parsed;
  };
  const createMarkup = () => {
    const parsed = parser(children);
    return writer.render(parsed).replace(/<a href="/g, `<a target="${linkTarget}" href="`).replace(/<p>/g, `<p class="${paragraphClass}">`);
  };
  return /*#__PURE__*/jsxRuntime.jsx(Element, {
    className: className,
    dangerouslySetInnerHTML: {
      __html: createMarkup()
    }
  });
}
function stripNodes({
  blockList,
  parsed
}) {
  if (!parsed) {
    return parsed;
  }
  const walker = parsed.walker();
  for (let event = walker.next(); event != null; event = walker.next()) {
    const {
      node
    } = event;
    if (blockList.includes(node.type) && !event.entering) {
      while (node.firstChild != null) {
        node.insertBefore(node.firstChild);
      }
      node.unlink();
    }
  }
  return parsed;
}

module.exports = Markdown;
//# sourceMappingURL=Markdown.js.map
