import commonmark from 'commonmark';
import { jsx } from 'react/jsx-runtime';
import { MarkdownNodeType } from '../common/propsValues/markdownNodeType.mjs';
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
