'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _styledComponents = require('styled-components');

var _styledComponents2 = _interopRequireDefault(_styledComponents);

var _SidebarListShowMore = require('./styles/SidebarListShowMore');

var _SidebarListShowMore2 = _interopRequireDefault(_SidebarListShowMore);

var _SidebarListItem = require('./SidebarListItem');

var _SidebarListItem2 = _interopRequireDefault(_SidebarListItem);

var _SidebarListContainer = require('./styles/SidebarListContainer');

var _SidebarListContainer2 = _interopRequireDefault(_SidebarListContainer);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var SidebarList = function (_React$Component) {
  _inherits(SidebarList, _React$Component);

  function SidebarList() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, SidebarList);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SidebarList.__proto__ || Object.getPrototypeOf(SidebarList)).call.apply(_ref, [this].concat(args))), _this), _this.renderPrevious = function () {
      var _this$props = _this.props,
          hasPreviousPage = _this$props.hasPreviousPage,
          onPreviousPageClicked = _this$props.onPreviousPageClicked,
          ShowMore = _this$props.ShowMore;

      if (hasPreviousPage) {
        return _react2.default.createElement(
          ShowMore,
          { onClick: onPreviousPageClicked },
          'Show Previous'
        );
      }

      return null;
    }, _this.renderNext = function () {
      var _this$props2 = _this.props,
          hasNextPage = _this$props2.hasNextPage,
          onNextPageClicked = _this$props2.onNextPageClicked,
          ShowMore = _this$props2.ShowMore;

      if (hasNextPage) {
        return _react2.default.createElement(
          ShowMore,
          { onClick: onNextPageClicked },
          'Show Next'
        );
      }

      return null;
    }, _this.renderItems = function () {
      var selectedIndex = _this.props.selectedIndex;


      return _react2.default.Children.toArray(_this.props.children).filter(function (child) {
        return child !== null;
      }).map(function (listItem, idx) {
        return _react2.default.cloneElement(listItem, {
          key: idx,
          active: idx === selectedIndex
        });
      });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(SidebarList, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          className = _props.className,
          id = _props.id,
          Container = _props.Container;

      return _react2.default.createElement(
        Container,
        { className: className, id: id },
        this.renderPrevious(),
        this.renderItems(),
        this.renderNext()
      );
    }
  }]);

  return SidebarList;
}(_react2.default.Component);

SidebarList.propTypes = {
  /**
   * Optional: indicate which item should be considered active.
   */
  selectedIndex: _propTypes2.default.number,
  /**
   * List items. An 'active' prop will be passed to the item that matches selectedIndex.
   */
  children: _propTypes2.default.node.isRequired,
  /**
   * Indicates whether the list should show that it has a next page.
   */
  hasNextPage: _propTypes2.default.bool,
  /**
   * Indicates whether the list should show that it has a previous page.
   */
  hasPreviousPage: _propTypes2.default.bool,
  /**
   * Called when the button to load the next page is clicked.
   */
  onNextPageClicked: _propTypes2.default.func,
  /**
   * Called when the button to load the previous page is clicked.
   */
  onPreviousPageClicked: _propTypes2.default.func,
  /**
   * Adds a class name to the containing list element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the containing list element.
   */
  id: _propTypes2.default.string,
  /**
   * A compoennt to render the container around the list
   */
  Container: _propTypes2.default.func,
  /**
   * A container that renders a clickable 'show more' area
   */
  ShowMore: _propTypes2.default.func
};
SidebarList.defaultProps = {
  selectedIndex: -1,
  hasNextPage: false,
  hasPreviousPage: false,
  onNextPageClicked: function onNextPageClicked() {
    return null;
  },
  onPreviousPageClicked: function onPreviousPageClicked() {
    return null;
  },
  className: null,
  id: null,
  Container: _SidebarListContainer2.default,
  ShowMore: _SidebarListShowMore2.default
};


SidebarList.Item = _SidebarListItem2.default;
exports.default = SidebarList;