import * as commonmark from 'commonmark';
import '../common/theme.mjs';
import '../common/direction.mjs';
import '../common/propsValues/control.mjs';
import '../common/propsValues/breakpoint.mjs';
import '../common/propsValues/size.mjs';
import '../common/propsValues/typography.mjs';
import '../common/propsValues/width.mjs';
import '../common/propsValues/type.mjs';
import '../common/propsValues/dateMode.mjs';
import '../common/propsValues/monthFormat.mjs';
import '../common/propsValues/position.mjs';
import '../common/propsValues/layouts.mjs';
import '../common/propsValues/status.mjs';
import '../common/propsValues/sentiment.mjs';
import '../common/propsValues/profileType.mjs';
import '../common/propsValues/variant.mjs';
import '../common/propsValues/scroll.mjs';
import { MarkdownNodeType } from '../common/propsValues/markdownNodeType.mjs';
import '../common/fileType.mjs';
import '@transferwise/icons';
import 'clsx';
import 'react';
import 'react-intl';
import '../common/closeButton/CloseButton.messages.mjs';
import { jsx } from 'react/jsx-runtime';
import { logActionRequired } from '../utilities/logActionRequired.mjs';

const reader = new commonmark.Parser();
const writer = new commonmark.HtmlRenderer({
  safe: true
});
const NODE_TYPE_LIST = Object.values(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('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__*/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;
}

export { Markdown as default };
//# sourceMappingURL=Markdown.mjs.map
