import EuiHorizontalRule from '../../../../components/eui-horizontal-rule.js';
import MarkdownCode from '../../../../components/eui-markdown-format/markdown-code.js';
import MarkdownCodeBlock from '../../../../components/eui-markdown-format/markdown-code-block.js';
import { FENCED_CLASS } from '../../remark/remark-prismjs.js';

const visit = (node, visitor) => {
  node = visitor(node);
  if (node) {
    let children = [];
    if (node.children) {
      node.children.forEach(child => {
        child = visit(child, visitor);
        if (child) {
          children.push(child);
        }
      });
      node.children = children;
    }
  }
  return node;
};
const processor = function MarkdownAddComponents() {
  return tree => {
    visit(tree, node => {
      if (node.tagName === 'component') {
        node.type = 'component';
      }
      if (node.type === 'element' && node.tagName === 'a') {
        node.properties.className = ['euiLink', 'euiLink--primary'];
      }
      if (node.type === 'element' && node.tagName === 'pre') {
        node.tagName = 'div';
        node.properties.className = ['euiMarkdownFormat__codeblockWrapper'];
      }
      if (node.type === 'element' && node.tagName === 'blockquote') {
        node.properties.className = ['euiMarkdownFormat__blockquote'];
      }
      if (node.type === 'element' && node.tagName === 'table') {
        node.properties.className = ['euiMarkdownFormat__table'];
      }
      if (node.type === 'element' && node.tagName === 'hr') {
        node.type = 'component';
        node.properties.componentName = EuiHorizontalRule;
      }
      if (node.type === 'element' && node.tagName === 'code') {
        node.type = 'component';
        const hasBreaks = node.children?.find(child => /\r|\n/.exec(child.value) || node.properties.className && node.properties.className.indexOf(FENCED_CLASS) > -1);
        if (hasBreaks) {
          //@ts-expect-error
          node.properties.componentName = MarkdownCodeBlock;
          node.properties['fontSize'] = 'm';
          node.properties['paddingSize'] = 's';
        } else {
          //@ts-expect-error
          node.properties.componentName = MarkdownCode;
          node.properties['inline'] = true;
        }
      }
      return node;
    });
  };
};

export { processor, visit };
//# sourceMappingURL=processor.js.map
