'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 _Icon = require('../Icon');

var _Icon2 = _interopRequireDefault(_Icon);

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 ITEM_SIZE = '30px';

var Container = _styledComponents2.default.ul.withConfig({
  displayName: 'Pagination__Container'
})(['display: inline-block;margin: 0;padding: 0;float: left;list-style: none;user-select: none;']);

var Item = _styledComponents2.default.li.withConfig({
  displayName: 'Pagination__Item'
})(['float: left;margin: 0;padding: 0;width: ', ';height: ', ';line-height: ', ';text-align: center;cursor: pointer;border: 1px solid ', ';border-right: none;user-select: none;', '&:active {background: ', ';}&:first-of-type {border-radius: 3px 0px 0px 3px;}&:last-of-type {border-radius: 0px 3px 3px 0px;border-right: 1px solid ', ';}'], ITEM_SIZE, ITEM_SIZE, ITEM_SIZE, function (_ref) {
  var theme = _ref.theme;
  return theme.colors.border;
}, function (_ref2) {
  var active = _ref2.active,
      theme = _ref2.theme;
  return active ? '\n      background: ' + theme.colors.primary + ';\n      border-color: ' + theme.colors.primary + ';\n      color: ' + theme.colors.white + ';\n\n      & + li {\n        border-left-color: ' + theme.colors.primary + ';\n      }\n    ' : '\n      &:hover {\n        background: ' + theme.colors.primaryLight + ';\n      }\n    ';
}, function (_ref3) {
  var theme = _ref3.theme;
  return theme.colors.lightGray;
}, function (_ref4) {
  var theme = _ref4.theme;
  return theme.colors.border;
});

var ItemPlaceholder = _styledComponents2.default.li.withConfig({
  displayName: 'Pagination__ItemPlaceholder'
})(['float: left;margin: 0;padding: 0;width: ', ';height: ', ';'], ITEM_SIZE, ITEM_SIZE);

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

  function Pagination() {
    var _ref5;

    var _temp, _this, _ret;

    _classCallCheck(this, Pagination);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref5 = Pagination.__proto__ || Object.getPrototypeOf(Pagination)).call.apply(_ref5, [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(
        Item,
        {
          onClick: _this.handlePreviousClick
        },
        _react2.default.createElement(_Icon2.default, { name: 'back' })
      ) : _react2.default.createElement(ItemPlaceholder, null);
    }, _this.renderNext = function () {
      return _this.props.currentPage < _this.props.pageCount - 1 ? _react2.default.createElement(
        Item,
        {
          onClick: _this.handleNextClick
        },
        _react2.default.createElement(_Icon2.default, { name: 'forward' })
      ) : _react2.default.createElement(ItemPlaceholder, null);
    }, _this.renderItems = function () {
      var _this$props3 = _this.props,
          pageCount = _this$props3.pageCount,
          currentPage = _this$props3.currentPage;

      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),
            active: pageNumber === currentPage
          },
          pageNumber + 1
        );
      });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Pagination, [{
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        Container,
        null,
        this.renderPrevious(),
        this.renderItems(),
        this.renderNext()
      );
    }
  }]);

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

Pagination.propTypes = {
  pageCount: _propTypes2.default.number,
  currentPage: _propTypes2.default.number,
  onPageSelected: _propTypes2.default.func.isRequired
};
Pagination.defaultProps = {
  pageCount: 1,
  currentPage: 0
};


Pagination.usage = '\n# Pagination\n\nRenders a \'controlled\' pagination container. You need to provide the current page and other metadata.\n\n```\n<Pagination pageCount={4} page={1} onPageSelected={(pageNumber) => { /* handle it */ }} />\n```\n';

exports.default = Pagination;