'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var renderers = require('./renderers.js');
var renderInlineTokens = require('./renderInlineTokens.js');
var renderTable = require('./renderTable.js');
var renderUtils = require('./renderUtils.js');

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

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

var renderBlockTokens = function renderBlockTokens(markedEngine, tokenRenderer) {
  var tokensList = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var context = arguments.length > 3 ? arguments[3] : undefined;
  var content = [];
  tokensList.forEach(function (token, index) {
    var _token$tokens;
    var key = "".concat(context.keyPrefix, "-").concat(index);
    switch (token.type) {
      case 'space':
      case 'def':
        return;
      case 'paragraph':
        {
          var children = renderInlineTokens.renderInlineTokens(markedEngine, tokenRenderer, token.tokens, "".concat(key, "-paragraph"));
          var defaultNode = renderUtils.renderElement(key, 'p', undefined, children);
          content.push(renderUtils.resolveTokenNode(tokenRenderer, token, defaultNode, children));
        }
        return;
      case 'heading':
        {
          var depth = Math.min(Math.max(token.depth, 1), 6);
          var tagName = "h".concat(depth);
          var _children = renderInlineTokens.renderInlineTokens(markedEngine, tokenRenderer, token.tokens, "".concat(key, "-heading"));
          var _defaultNode = renderUtils.renderElement(key, tagName, undefined, _children);
          content.push(renderUtils.resolveTokenNode(tokenRenderer, token, _defaultNode, _children));
          return;
        }
      case 'blockquote':
        {
          var _children2 = renderBlockTokens(markedEngine, tokenRenderer, token.tokens, {
            keyPrefix: "".concat(key, "-blockquote")
          });
          var _defaultNode2 = renderUtils.renderElement(key, 'blockquote', undefined, _children2);
          content.push(renderUtils.resolveTokenNode(tokenRenderer, token, _defaultNode2, _children2));
        }
        return;
      case 'code':
        {
          var _children3 = token.text;
          var _defaultNode3 = renderers.renderCode(key, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, token), {}, {
            children: _children3,
            className: token.lang ? "language-".concat(token.lang) : undefined,
            inline: false
          }));
          content.push(renderUtils.resolveTokenNode(tokenRenderer, token, _defaultNode3, _children3));
        }
        return;
      case 'list':
        {
          var listToken = token;
          var listTag = listToken.ordered ? 'ol' : 'ul';
          var start = listToken.ordered && listToken.start !== '' ? Number(listToken.start) : undefined;
          var _children4 = listToken.items.map(function (item, itemIndex) {
            var itemChildren = renderBlockTokens(markedEngine, tokenRenderer, item.tokens, {
              inTightListItem: !item.loose,
              keyPrefix: "".concat(key, "-item-").concat(itemIndex)
            });
            var childContent = itemChildren;
            if (item.task) {
              childContent = [/*#__PURE__*/React__default.default.createElement("input", {
                checked: Boolean(item.checked),
                key: "".concat(key, "-item-").concat(itemIndex, "-checkbox"),
                type: "checkbox",
                disabled: true,
                readOnly: true
              }), ' '].concat(_rollupPluginBabelHelpers.toConsumableArray(itemChildren));
            }
            var itemDefaultNode = renderUtils.renderElement("".concat(key, "-li-").concat(itemIndex), 'li', undefined, childContent);
            return renderUtils.resolveTokenNode(tokenRenderer, item, itemDefaultNode, childContent);
          });
          var _defaultNode4 = renderUtils.renderElement(key, listTag, start === undefined ? undefined : {
            start: start
          }, _children4);
          content.push(renderUtils.resolveTokenNode(tokenRenderer, token, _defaultNode4, _children4));
          return;
        }
      case 'hr':
        content.push(renderUtils.resolveTokenNode(tokenRenderer, token, renderUtils.renderElement(key, 'hr')));
        return;
      case 'table':
        content.push(renderTable.renderTable(markedEngine, tokenRenderer, token, key));
        return;
      case 'html':
        content.push(renderUtils.renderSanitizedHtml(key, token.raw, token.block ? 'div' : 'span'));
        return;
      case 'text':
        if ((_token$tokens = token.tokens) !== null && _token$tokens !== void 0 && _token$tokens.length) {
          var textContent = renderInlineTokens.renderInlineTokens(markedEngine, tokenRenderer, token.tokens, "".concat(key, "-text"));
          if (context.inTightListItem) {
            content.push(/*#__PURE__*/React__default.default.createElement(React.Fragment, {
              key: key
            }, textContent));
          } else {
            content.push(renderUtils.renderElement(key, 'p', undefined, textContent));
          }
          return;
        }
        if (context.inTightListItem) {
          content.push(/*#__PURE__*/React__default.default.createElement(React.Fragment, {
            key: key
          }, token.text));
        } else {
          content.push(renderUtils.renderElement(key, 'p', undefined, token.text));
        }
        return;
      default:
        {
          var customToken = renderInlineTokens.renderCustomToken(markedEngine, tokenRenderer, token, key);
          if (customToken) {
            content.push(customToken);
            return;
          }
        }
        if ('tokens' in token && Array.isArray(token.tokens)) {
          content.push.apply(content, _rollupPluginBabelHelpers.toConsumableArray(renderBlockTokens(markedEngine, tokenRenderer, token.tokens, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, {
            keyPrefix: "".concat(key, "-fallback")
          }))));
        }
    }
  });
  return content;
};

exports.renderBlockTokens = renderBlockTokens;
