'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Icon = require('../Icon');

var _Icon2 = _interopRequireDefault(_Icon);

var _PaginationContainer = require('./styles/PaginationContainer');

var _PaginationContainer2 = _interopRequireDefault(_PaginationContainer);

var _PaginationItem = require('./styles/PaginationItem');

var _PaginationItem2 = _interopRequireDefault(_PaginationItem);

var _PaginationItemPlaceholder = require('./styles/PaginationItemPlaceholder');

var _PaginationItemPlaceholder2 = _interopRequireDefault(_PaginationItemPlaceholder);

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

var Pagination = function (_React$Component) {
  (0, _inherits3.default)(Pagination, _React$Component);

  function Pagination() {
    var _ref,
        _this2 = this;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, Pagination);

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

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Pagination.__proto__ || (0, _getPrototypeOf2.default)(Pagination)).call.apply(_ref, [this].concat(args))), _this), _this.createItemClickHandler = function (index) {
      return function () {
        return _this.props.onPageSelected(index);
      };
    }, _this.handlePreviousClick = function () {
      var _this$props = _this.props,
          onPageSelected = _this$props.onPageSelected,
          currentPage = _this$props.currentPage;

      if (currentPage === 0) {
        return;
      }
      onPageSelected(currentPage - 1);
    }, _this.handleNextClick = function () {
      var _this$props2 = _this.props,
          onPageSelected = _this$props2.onPageSelected,
          currentPage = _this$props2.currentPage,
          pageCount = _this$props2.pageCount;

      if (currentPage === pageCount - 1) {
        return;
      }

      onPageSelected(currentPage + 1);
    }, _this.renderPrevious = function () {
      return _this.props.currentPage > 0 ? _react2.default.createElement(
        _this2.props.Item,
        { onClick: _this.handlePreviousClick },
        _react2.default.createElement(_Icon2.default, { name: 'back' })
      ) : _react2.default.createElement(_this2.props.ItemPlaceholder, null);
    }, _this.renderNext = function () {
      return _this.props.currentPage < _this.props.pageCount - 1 ? _react2.default.createElement(
        _this2.props.Item,
        { onClick: _this.handleNextClick },
        _react2.default.createElement(_Icon2.default, { name: 'forward' })
      ) : _react2.default.createElement(_this2.props.ItemPlaceholder, null);
    }, _this.renderItems = function () {
      var _this$props3 = _this.props,
          pageCount = _this$props3.pageCount,
          currentPage = _this$props3.currentPage,
          Item = _this$props3.Item;

      var start = Math.max(0, Math.min(currentPage + 5, pageCount) - 10);
      var end = Math.min(start + 10, pageCount);
      return new Array(pageCount).fill(null)
      // creates an array of incrementing numbers
      .map(function (_, index) {
        return index;
      }).slice(start, end).map(function (pageNumber) {
        return _react2.default.createElement(
          Item,
          {
            key: pageNumber,
            onClick: _this.createItemClickHandler(pageNumber),
            selected: pageNumber === currentPage
          },
          pageNumber + 1
        );
      });
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(Pagination, [{
    key: 'render',
    value: function render() {
      var _props = this.props,
          id = _props.id,
          className = _props.className,
          Container = _props.Container;

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

Pagination.propTypes = {
  /**
   * The number of total pages available.
   */
  pageCount: _propTypes2.default.number,
  /**
   * The currently selected page index.
   */
  currentPage: _propTypes2.default.number,
  /**
   * A callback for page selection. The callback is passed the new selected page index.
   */
  onPageSelected: _propTypes2.default.func.isRequired,
  /**
   * Adds a class name to the pagination container element.
   */
  className: _propTypes2.default.string,
  /**
   * Adds an id to the pagination container element.
   */
  id: _propTypes2.default.string,
  /**
   * A component to render a container for the pagination
   */
  Container: _propTypes2.default.func,
  /**
   * A component to render a single pagination item
   */
  Item: _propTypes2.default.func,
  /**
   * A component to render a placeholder space for a pagination item
   */
  ItemPlaceholder: _propTypes2.default.func
};
Pagination.defaultProps = {
  pageCount: 1,
  currentPage: 0,
  className: null,
  id: null,
  Container: _PaginationContainer2.default,
  Item: _PaginationItem2.default,
  ItemPlaceholder: _PaginationItemPlaceholder2.default
};
exports.default = Pagination;