'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var reactSyntaxHighlighter = require('react-syntax-highlighter');
var Markdown_tokens = require('../Markdown.tokens.js');
var Markdown_styles = require('../Markdown.styles.js');
var sanitizeUrl = require('../utils/sanitizeUrl.js');
var renderUtils = require('./renderUtils.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);

var getCodeLanguage = function getCodeLanguage(className) {
  if (!className) {
    return undefined;
  }
  var match = className.match(/(?:lang(?:uage)?-)([a-z0-9+#.-]+)/i);
  return match && match[1] ? match[1].toLowerCase() : undefined;
};
var CodeRenderer = function CodeRenderer(_ref) {
  var children = _ref.children,
    className = _ref.className,
    inline = _ref.inline;
  var value = String(children || '').replace(/\n$/, '');
  if (inline) {
    return /*#__PURE__*/React__default.default.createElement(Markdown_styles.InlineCode, {
      className: className
    }, children);
  }
  var language = getCodeLanguage(className) || 'text';
  return /*#__PURE__*/React__default.default.createElement(Markdown_styles.CodeBlock, null, /*#__PURE__*/React__default.default.createElement(Markdown_styles.CodeBlockHeader, null, /*#__PURE__*/React__default.default.createElement(Markdown_styles.CodeLanguage, null, language)), /*#__PURE__*/React__default.default.createElement(Markdown_styles.CodeBlockBody, null, /*#__PURE__*/React__default.default.createElement(reactSyntaxHighlighter.Prism, {
    PreTag: "div",
    codeTagProps: {
      style: {
        fontFamily: 'SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace'
      }
    },
    customStyle: {
      margin: 0,
      padding: "var(".concat(Markdown_tokens.tokens.codeBlockPadding, ", 1rem 1.125rem)")
    },
    language: language,
    style: Markdown_styles.syntaxTheme,
    wrapLongLines: true
  }, value)));
};
var renderCode = function renderCode(key, props) {
  return renderUtils.renderElement(key, CodeRenderer, props, props.children);
};
var ImageRenderer = function ImageRenderer(_ref2) {
  var alt = _ref2.alt,
    _ref2$imageDisplay = _ref2.imageDisplay,
    imageDisplay = _ref2$imageDisplay === void 0 ? 'inline' : _ref2$imageDisplay,
    src = _ref2.src,
    title = _ref2.title;
  var sanitizedSrc = sanitizeUrl.sanitizeUrl(src);
  if (!sanitizedSrc) {
    return null;
  }
  var Image = imageDisplay === 'block' ? Markdown_styles.StyledBlockImage : Markdown_styles.StyledInlineImage;
  return /*#__PURE__*/React__default.default.createElement(Image, {
    alt: alt || '',
    loading: "lazy",
    src: sanitizedSrc,
    title: title
  });
};
var LinkRenderer = function LinkRenderer(_ref3) {
  var href = _ref3.href,
    children = _ref3.children,
    title = _ref3.title;
  var sanitizedHref = sanitizeUrl.sanitizeUrl(href);
  var isExternal = typeof sanitizedHref === 'string' && /^https?:\/\//.test(sanitizedHref);
  return /*#__PURE__*/React__default.default.createElement("a", {
    href: sanitizedHref,
    rel: isExternal ? 'noreferrer noopener' : undefined,
    target: isExternal ? '_blank' : undefined,
    title: title
  }, children);
};
var TableRenderer = function TableRenderer(_ref4) {
  var children = _ref4.children;
  return /*#__PURE__*/React__default.default.createElement(Markdown_styles.TableScroll, null, /*#__PURE__*/React__default.default.createElement("table", null, children));
};

exports.ImageRenderer = ImageRenderer;
exports.LinkRenderer = LinkRenderer;
exports.TableRenderer = TableRenderer;
exports.renderCode = renderCode;
