'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

require('./style.css');

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

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

OrderedList.propTypes = {
  /** The text displayed. */
  sublist: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    text: _propTypes2.default.string.required,
    sublist: _propTypes2.default.arrayOf(_propTypes2.default.shape({
      text: _propTypes2.default.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 _react2.default.createElement('li', { key: key, dangerouslySetInnerHTML: raw });
};

listItem.propTypes = process.env.NODE_ENV !== "production" ? {
  text: _propTypes2.default.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 _react2.default.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);
    })
  );
};

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