import { objectWithoutProperties as _objectWithoutProperties, construct as _construct, toConsumableArray as _toConsumableArray, extends as _extends } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useMemo } from 'react';
import { Marked } from 'marked';
import { base as base$1 } from './variations/_view/base.js';
import { base as base$2 } from './variations/_size/base.js';
import { base } from './Markdown.styles.js';
import { normalizeIncompleteMarkdown } from './utils/normalizeIncompleteMarkdown.js';
import { renderBlockTokens } from './renderers/renderBlockTokens.js';

var _excluded = ["content", "tokenRenderer", "markedOptions", "shouldParseIncompleteMarkdown", "size", "view"];
var markdownRoot = function markdownRoot(Root) {
  return /*#__PURE__*/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 = _objectWithoutProperties(_ref, _excluded);
    var normalizedContent = useMemo(function () {
      return normalizeIncompleteMarkdown(content, shouldParseIncompleteMarkdown);
    }, [content, shouldParseIncompleteMarkdown]);
    var marked = useMemo(function () {
      return _construct(Marked, [{
        gfm: true
      }].concat(_toConsumableArray(markedOptions ? [markedOptions] : [])));
    }, [markedOptions]);
    var tokenList = useMemo(function () {
      return marked.lexer(normalizedContent);
    }, [marked, normalizedContent]);
    var renderedContent = useMemo(function () {
      return renderBlockTokens(marked, tokenRenderer, tokenList, {
        keyPrefix: 'markdown'
      });
    }, [marked, tokenRenderer, tokenList]);
    return /*#__PURE__*/React__default.createElement(Root, _extends({
      ref: ref,
      size: size,
      view: view
    }, rest), renderedContent);
  });
};
var markdownConfig = {
  name: 'Markdown',
  tag: 'div',
  layout: markdownRoot,
  base: base,
  variations: {
    view: {
      css: base$1
    },
    size: {
      css: base$2
    }
  },
  defaults: {
    view: 'default',
    size: 'm'
  }
};

export { markdownConfig, markdownRoot };
