'use strict';

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

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var marked = require('marked');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var Markdown_styles = require('./Markdown.styles.js');
var normalizeIncompleteMarkdown = require('./utils/normalizeIncompleteMarkdown.js');
var renderBlockTokens = require('./renderers/renderBlockTokens.js');

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

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

var _excluded = ["content", "tokenRenderer", "markedOptions", "shouldParseIncompleteMarkdown", "size", "view"];
var markdownRoot = function markdownRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, ref) {
    var _ref$content = _ref.content,
      content = _ref$content === void 0 ? '' : _ref$content,
      tokenRenderer = _ref.tokenRenderer,
      markedOptions = _ref.markedOptions,
      _ref$shouldParseIncom = _ref.shouldParseIncompleteMarkdown,
      shouldParseIncompleteMarkdown = _ref$shouldParseIncom === void 0 ? true : _ref$shouldParseIncom,
      size = _ref.size,
      view = _ref.view,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var normalizedContent = React.useMemo(function () {
      return normalizeIncompleteMarkdown.normalizeIncompleteMarkdown(content, shouldParseIncompleteMarkdown);
    }, [content, shouldParseIncompleteMarkdown]);
    var marked$1 = React.useMemo(function () {
      return _rollupPluginBabelHelpers.construct(marked.Marked, [{
        gfm: true
      }].concat(_rollupPluginBabelHelpers.toConsumableArray(markedOptions ? [markedOptions] : [])));
    }, [markedOptions]);
    var tokenList = React.useMemo(function () {
      return marked$1.lexer(normalizedContent);
    }, [marked$1, normalizedContent]);
    var renderedContent = React.useMemo(function () {
      return renderBlockTokens.renderBlockTokens(marked$1, tokenRenderer, tokenList, {
        keyPrefix: 'markdown'
      });
    }, [marked$1, tokenRenderer, tokenList]);
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: ref,
      size: size,
      view: view
    }, rest), renderedContent);
  });
};
var markdownConfig = {
  name: 'Markdown',
  tag: 'div',
  layout: markdownRoot,
  base: Markdown_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

exports.markdownConfig = markdownConfig;
exports.markdownRoot = markdownRoot;
