'use strict';

exports.__esModule = true;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _recompose = require('recompose');

var _PaginationButton = require('./PaginationButton');

var _PaginationButton2 = _interopRequireDefault(_PaginationButton);

var _SafeAnchor = require('./SafeAnchor');

var _SafeAnchor2 = _interopRequireDefault(_SafeAnchor);

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

var _Icon2 = _interopRequireDefault(_Icon);

var _withLocale = require('./IntlProvider/withLocale');

var _withLocale2 = _interopRequireDefault(_withLocale);

var _utils = require('./utils');

var _constants = require('./utils/constants');

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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) : _defaults(subClass, superClass); }

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

  function Pagination() {
    _classCallCheck(this, Pagination);

    return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  }

  /**
   * Note that `handledProps` are generated automatically during
   * build with `babel-plugin-transform-react-flow-handled-props`
   */
  Pagination.prototype.renderPageButtons = function renderPageButtons() {
    var pageButtons = [];
    var startPage = void 0;
    var endPage = void 0;
    var hasHiddenPagesAfter = void 0;

    var _props = this.props,
        maxButtons = _props.maxButtons,
        activePage = _props.activePage,
        pages = _props.pages,
        ellipsis = _props.ellipsis,
        boundaryLinks = _props.boundaryLinks,
        locale = _props.locale;


    if (maxButtons) {
      var hiddenPagesBefore = activePage - parseInt(maxButtons / 2, 10);
      startPage = hiddenPagesBefore > 1 ? hiddenPagesBefore : 1;
      hasHiddenPagesAfter = startPage + maxButtons <= pages;

      if (!hasHiddenPagesAfter) {
        endPage = pages;
        startPage = pages - maxButtons + 1;
        if (startPage < 1) {
          startPage = 1;
        }
      } else {
        endPage = startPage + maxButtons - 1;
      }
    } else {
      startPage = 1;
      endPage = pages;
    }

    for (var pagenumber = startPage; pagenumber <= endPage; pagenumber += 1) {
      pageButtons.push(this.renderItem({
        key: pagenumber,
        eventKey: pagenumber,
        active: pagenumber === activePage,
        children: pagenumber
      }));
    }

    if (boundaryLinks && ellipsis && startPage !== 1) {
      pageButtons.unshift(this.renderItem({
        key: 'ellipsisFirst',
        disabled: true,
        children: React.createElement(
          'span',
          { 'aria-label': 'More' },
          ellipsis === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.more }) : ellipsis
        )
      }));

      pageButtons.unshift(this.renderItem({
        key: 1,
        eventKey: 1,
        children: 1
      }));
    }

    if (maxButtons && hasHiddenPagesAfter && ellipsis) {
      pageButtons.push(this.renderItem({
        key: 'ellipsis',
        disabled: true,
        children: React.createElement(
          'span',
          { 'aria-label': 'More', title: locale.more },
          ellipsis === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.more }) : ellipsis
        )
      }));

      if (boundaryLinks && endPage !== pages) {
        pageButtons.push(this.renderItem({
          key: pages,
          eventKey: pages,
          disabled: false,
          children: pages
        }));
      }
    }
    return pageButtons;
  };

  Pagination.prototype.renderPrev = function renderPrev() {
    var _props2 = this.props,
        activePage = _props2.activePage,
        prev = _props2.prev,
        locale = _props2.locale;


    if (!prev) {
      return null;
    }

    return this.renderItem({
      key: 'prev',
      eventKey: activePage - 1,
      disabled: activePage === 1,
      children: React.createElement(
        'span',
        { 'aria-label': 'Previous', title: locale.prev },
        prev === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.prev }) : prev
      )
    });
  };

  Pagination.prototype.renderNext = function renderNext() {
    var _props3 = this.props,
        pages = _props3.pages,
        activePage = _props3.activePage,
        next = _props3.next,
        locale = _props3.locale;


    if (!next) {
      return null;
    }

    return this.renderItem({
      key: 'next',
      eventKey: activePage + 1,
      disabled: activePage >= pages,
      children: React.createElement(
        'span',
        { 'aria-label': 'Next', title: locale.next },
        next === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.next }) : next
      )
    });
  };

  Pagination.prototype.renderFirst = function renderFirst() {
    var _props4 = this.props,
        activePage = _props4.activePage,
        first = _props4.first,
        locale = _props4.locale;


    if (!first) {
      return null;
    }

    return this.renderItem({
      key: 'first',
      eventKey: 1,
      disabled: activePage === 1,
      children: React.createElement(
        'span',
        { 'aria-label': 'First', title: locale.first },
        first === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.first }) : first
      )
    });
  };

  Pagination.prototype.renderLast = function renderLast() {
    var _props5 = this.props,
        pages = _props5.pages,
        activePage = _props5.activePage,
        last = _props5.last,
        locale = _props5.locale;

    if (!last) {
      return null;
    }

    return this.renderItem({
      key: 'last',
      eventKey: pages,
      disabled: activePage >= pages,
      children: React.createElement(
        'span',
        { 'aria-label': 'Last', title: locale.last },
        last === true ? React.createElement(_Icon2.default, { icon: _constants.PAGINATION_ICON_NAMES.last }) : last
      )
    });
  };

  Pagination.prototype.renderItem = function renderItem(props) {
    var _props6 = this.props,
        onSelect = _props6.onSelect,
        buttonComponentClass = _props6.buttonComponentClass;

    return React.createElement(_PaginationButton2.default, _extends({}, props, {
      onSelect: props.disabled ? null : onSelect,
      componentClass: buttonComponentClass
    }));
  };

  Pagination.prototype.render = function render() {
    var _props7 = this.props,
        className = _props7.className,
        classPrefix = _props7.classPrefix,
        rest = _objectWithoutProperties(_props7, ['className', 'classPrefix']);

    var unhandled = (0, _utils.getUnhandledProps)(Pagination, rest);

    return React.createElement(
      'ul',
      _extends({ className: (0, _classnames2.default)(classPrefix, className) }, unhandled),
      this.renderFirst(),
      this.renderPrev(),
      this.renderPageButtons(),
      this.renderNext(),
      this.renderLast()
    );
  };

  return Pagination;
}(React.Component);

Pagination.defaultProps = {
  activePage: 1,
  pages: 1,
  maxButtons: 0,
  buttonComponentClass: _SafeAnchor2.default,
  locale: {
    more: 'More',
    prev: 'Previous',
    next: 'Next',
    first: 'First',
    last: 'Last'
  }
};
Pagination.handledProps = ['activePage', 'boundaryLinks', 'buttonComponentClass', 'className', 'classPrefix', 'ellipsis', 'first', 'last', 'locale', 'maxButtons', 'next', 'onSelect', 'pages', 'prev'];
Pagination.propTypes = {
  activePage: _propTypes2.default.number.isRequired,
  pages: _propTypes2.default.number.isRequired,
  maxButtons: _propTypes2.default.number.isRequired,
  boundaryLinks: _propTypes2.default.bool,
  ellipsis: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.node]),
  first: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.node]),
  last: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.node]),
  prev: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.node]),
  next: _propTypes2.default.oneOfType([_propTypes2.default.bool, _propTypes2.default.node]),
  onSelect: _propTypes2.default.func,
  buttonComponentClass: _propTypes2.default.oneOfType([function () {
    return (typeof (React.ElementType == null ? {} : React.ElementType) === 'function' ? _propTypes2.default.instanceOf(React.ElementType == null ? {} : React.ElementType) : _propTypes2.default.any).apply(this, arguments);
  }, _propTypes2.default.string]).isRequired,
  className: _propTypes2.default.string,
  classPrefix: _propTypes2.default.string.isRequired,
  locale: _propTypes2.default.shape({
    more: _propTypes2.default.string,
    prev: _propTypes2.default.string,
    next: _propTypes2.default.string,
    first: _propTypes2.default.string,
    last: _propTypes2.default.string
  }).isRequired
};
exports.default = (0, _recompose.compose)((0, _withLocale2.default)(['Pagination']), (0, _utils.withStyleProps)({ hasSize: true }), (0, _utils.defaultProps)({
  classPrefix: 'pagination'
}))(Pagination);