'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _object = require('object.entries');

var _object2 = _interopRequireDefault(_object);

var _index = require('../../atoms/links/DecorativeLink/index');

var _index2 = _interopRequireDefault(_index);

var _index3 = require('../../atoms/headings/CompHeading/index');

var _index4 = _interopRequireDefault(_index3);

var _index5 = require('../../atoms/headings/SidebarHeading/index');

var _index6 = _interopRequireDefault(_index5);

var _Paragraph = require('../../atoms/text/Paragraph');

var _Paragraph2 = _interopRequireDefault(_Paragraph);

var _UnorderedList = require('../../atoms/lists/UnorderedList');

var _UnorderedList2 = _interopRequireDefault(_UnorderedList);

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// import child components
var RichText = function RichText(props) {
  if (!Object.entries) {
    _object2.default.shim();
  }
  var headerIndent = props.headerIndent ? 'js-ma-outline-indent' : '';
  var anchorLinks = props.anchorLinks ? 'js-ma-insert-heading-anchors' : '';
  var children = _react2.default.Children.toArray(props.children);
  var optional = ['CompHeading', 'DecorativeLink', 'SidebarHeading'];
  var elementProperties = {
    paragraph: function paragraph(value) {
      return _react2.default.createElement(_Paragraph2.default, value);
    },
    unorderedList: function unorderedList(value) {
      return _react2.default.createElement(_UnorderedList2.default, value);
    }
  };
  var rteElements = props.rteElements || null;
  var requiredElements = [];
  if (typeof rteElements !== 'undefined' && rteElements !== null) {
    requiredElements = rteElements.map(function (element) {
      if (typeof element.data === 'undefined' || element.data === null) {
        return [];
      }
      return Object.entries(element.data).map(function (_ref) {
        var eleIndex = _ref[0],
            eleValue = _ref[1];

        if (Object.prototype.hasOwnProperty.call(elementProperties, eleIndex)) {
          var newEleValue = Object.assign({}, eleValue);
          newEleValue.key = 'element.' + eleIndex;
          return elementProperties[eleIndex](newEleValue);
        }
        return null;
      });
    });
  }
  var optionalElements = [];
  children.forEach(function (value) {
    if (optional.indexOf(value.type.name) >= 0) {
      optionalElements[value.type.name] = value;
    } else {
      requiredElements.push(value);
    }
  });
  var decorative = optionalElements.DecorativeLink || null;
  return _react2.default.createElement(
    'section',
    { className: 'ma__rich-text js-ma-rich-text ' + headerIndent + ' ' + anchorLinks },
    optionalElements.CompHeading,
    optionalElements.SidebarHeading,
    requiredElements,
    decorative && _react2.default.createElement(
      'div',
      { className: 'ma__rich-text__more' },
      decorative
    )
  );
};

RichText.propTypes = process.env.NODE_ENV !== "production" ? {
  headerIndent: _propTypes2.default.bool,
  anchorLinks: _propTypes2.default.bool,
  /** @atoms/headings/CompHeading */
  compHeading: _propTypes2.default.instanceOf(_index4.default),
  /** @atoms/headings/SidebarHeading */
  sidebarHeading: _propTypes2.default.instanceOf(_index6.default),
  /** @atoms/links/DecorativeLink */
  decorativeLink: _propTypes2.default.instanceOf(_index2.default),
  rteElements: _propTypes2.default.arrayOf(_propTypes2.default.object),
  /** Children passed to rich text. */
  children: _propTypes2.default.node
} : {};

RichText.defaultProps = {
  headerIndent: false,
  anchorLinks: false,
  rteElements: []
};

exports.default = RichText;
module.exports = exports['default'];