import React from 'react';
import PropTypes from 'prop-types';
import './style.css';

var OrderedList = function OrderedList(props) {
  return subList(props);
};

OrderedList.propTypes = {
  /** The text displayed. */
  sublist: PropTypes.arrayOf(PropTypes.shape({
    text: PropTypes.string.required,
    sublist: PropTypes.arrayOf(PropTypes.shape({
      text: PropTypes.string.required
    }))
  }))
};

OrderedList.defaultProps = {
  sublist: [{
    text: 'This is a list item in an ordered list'
  }, {
    text: 'An ordered list is a list in which the sequence of items is important.'
  }, {
    text: 'Lists can be nested inside of each other',
    sublist: [{
      text: 'This is a nested list item'
    }, {
      text: 'This is another nested list item in an ordered list'
    }]
  }, {
    text: 'This is the last list item'
  }]
};

var listItem = function listItem(props, itemIndex, olIndex) {
  var raw = {
    __html: props.text
  };
  var key = 'li.' + olIndex + '.' + itemIndex;
  /* eslint-disable-next-line react/no-danger */
  return React.createElement('li', { key: key, dangerouslySetInnerHTML: raw });
};

listItem.propTypes = process.env.NODE_ENV !== "production" ? {
  text: PropTypes.string
} : {};

var subList = function subList(props) {
  var newProps = Object.assign({}, props);
  if (!Object.prototype.hasOwnProperty.call(newProps, 'olIndex')) {
    newProps.olIndex = 0;
  }
  var olId = 'ol.' + newProps.olIndex;
  newProps.olIndex += 1;
  var list = newProps.sublist;
  return React.createElement(
    'ol',
    { key: olId },
    list && list.map(function (item, itemIndex) {
      if (item.sublist) {
        var subitem = [listItem(item, itemIndex, olId)];
        subitem.push(subList(item));
        return subitem;
      }
      return listItem(item, itemIndex, olId);
    })
  );
};

export default OrderedList;