import React from 'react';
import PropTypes from 'prop-types';
import entries from 'object.entries';
// import child components
import DecorativeLink from '../../atoms/links/DecorativeLink/index';
import CompHeading from '../../atoms/headings/CompHeading/index';
import SidebarHeading from '../../atoms/headings/SidebarHeading/index';
import Paragraph from '../../atoms/text/Paragraph';
import UnorderedList from '../../atoms/lists/UnorderedList';

import './style.css';

var RichText = function RichText(props) {
  if (!Object.entries) {
    entries.shim();
  }
  var headerIndent = props.headerIndent ? 'js-ma-outline-indent' : '';
  var anchorLinks = props.anchorLinks ? 'js-ma-insert-heading-anchors' : '';
  var children = React.Children.toArray(props.children);
  var optional = ['CompHeading', 'DecorativeLink', 'SidebarHeading'];
  var elementProperties = {
    paragraph: function paragraph(value) {
      return React.createElement(Paragraph, value);
    },
    unorderedList: function unorderedList(value) {
      return React.createElement(UnorderedList, 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 React.createElement(
    'section',
    { className: 'ma__rich-text js-ma-rich-text ' + headerIndent + ' ' + anchorLinks },
    optionalElements.CompHeading,
    optionalElements.SidebarHeading,
    requiredElements,
    decorative && React.createElement(
      'div',
      { className: 'ma__rich-text__more' },
      decorative
    )
  );
};

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

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

export default RichText;