'use strict';

exports.__esModule = true;

var _react = require('react');

var React = _interopRequireWildcard(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _compose = require('recompose/compose');

var _compose2 = _interopRequireDefault(_compose);

var _Pagination = require('./Pagination');

var _Pagination2 = _interopRequireDefault(_Pagination);

var _SelectPicker = require('./SelectPicker');

var _SelectPicker2 = _interopRequireDefault(_SelectPicker);

var _Divider = require('./Divider');

var _Divider2 = _interopRequireDefault(_Divider);

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

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

var _withLocale2 = _interopRequireDefault(_withLocale);

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 _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 TablePagination = function (_React$Component) {
  _inherits(TablePagination, _React$Component);

  function TablePagination(props) {
    _classCallCheck(this, TablePagination);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.handleChangeLength = function (eventKey) {
      var onChangeLength = _this.props.onChangeLength;

      _this.setState({
        displayLength: eventKey
      });
      onChangeLength && onChangeLength(eventKey);
    };

    _this.handleChangePage = function (eventKey) {
      var onChangePage = _this.props.onChangePage;

      _this.setState({
        activePage: eventKey
      });
      onChangePage && onChangePage(eventKey);
    };

    _this.addPrefix = function (name) {
      return (0, _utils.prefix)(_this.props.classPrefix)(name);
    };

    _this.state = {
      displayLength: props.displayLength,
      activePage: props.activePage
    };
    return _this;
  }

  TablePagination.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var _props = this.props,
        displayLength = _props.displayLength,
        activePage = _props.activePage;

    if (displayLength !== nextProps.displayLength) {
      this.setState({
        displayLength: nextProps.displayLength
      });
    }

    if (activePage !== nextProps.activePage) {
      this.setState({
        activePage: nextProps.activePage
      });
    }
  };

  TablePagination.prototype.renderLengthMenu = function renderLengthMenu() {
    var _props2 = this.props,
        _props2$lengthMenu = _props2.lengthMenu,
        lengthMenu = _props2$lengthMenu === undefined ? [] : _props2$lengthMenu,
        renderLengthMenu = _props2.renderLengthMenu,
        showLengthMenu = _props2.showLengthMenu,
        locale = _props2.locale;
    var displayLength = this.state.displayLength;


    if (!showLengthMenu) {
      return null;
    }

    var picker = React.createElement(_SelectPicker2.default, {
      cleanable: false,
      searchable: false,
      placement: 'topLeft',
      data: lengthMenu,
      value: displayLength,
      onChange: this.handleChangeLength
    });

    return React.createElement(
      'div',
      { className: this.addPrefix('length-menu') },
      renderLengthMenu ? renderLengthMenu(picker) : (0, _utils.tplTransform)(locale.lengthMenuInfo, picker)
    );
  };

  TablePagination.prototype.renderInfo = function renderInfo() {
    var _props3 = this.props,
        renderTotal = _props3.renderTotal,
        total = _props3.total,
        showInfo = _props3.showInfo,
        locale = _props3.locale;


    if (!showInfo) {
      return null;
    }

    var activePage = this.state.activePage;

    return React.createElement(
      'div',
      { className: this.addPrefix('page-info') },
      renderTotal ? renderTotal(total, activePage) : (0, _utils.tplTransform)(locale.totalInfo, total)
    );
  };

  TablePagination.prototype.render = function render() {
    var _props4 = this.props,
        total = _props4.total,
        prev = _props4.prev,
        next = _props4.next,
        first = _props4.first,
        last = _props4.last,
        maxButtons = _props4.maxButtons,
        className = _props4.className;
    var _state = this.state,
        displayLength = _state.displayLength,
        activePage = _state.activePage;

    var pages = Math.floor(total / displayLength) + (total % displayLength ? 1 : 0);
    var classes = (0, _classnames2.default)(this.addPrefix('pagination-wrapper'), className);

    return React.createElement(
      'div',
      { className: classes },
      this.renderLengthMenu(),
      React.createElement(_Divider2.default, { vertical: true }),
      this.renderInfo(),
      React.createElement(
        'div',
        { className: (0, _classnames2.default)(this.addPrefix('pagination')) },
        React.createElement(_Pagination2.default, {
          size: 'xs',
          prev: prev,
          next: next,
          first: first,
          last: last,
          maxButtons: maxButtons,
          pages: pages,
          onSelect: this.handleChangePage,
          activePage: activePage
        })
      )
    );
  };

  return TablePagination;
}(React.Component);

TablePagination.defaultProps = {
  showLengthMenu: true,
  showInfo: true,
  lengthMenu: [{
    value: 30,
    label: 30
  }, {
    value: 50,
    label: 50
  }, {
    value: 100,
    label: 100
  }],
  displayLength: 30,
  prev: true,
  next: true,
  first: true,
  last: true,
  activePage: 1,
  maxButtons: 5,
  locale: {
    lengthMenuInfo: 'Show {0} data',
    totalInfo: 'Total: {0}'
  }
};
TablePagination.handledProps = ['activePage', 'className', 'classPrefix', 'displayLength', 'first', 'last', 'lengthMenu', 'locale', 'maxButtons', 'next', 'onChangeLength', 'onChangePage', 'prev', 'renderLengthMenu', 'renderTotal', 'showInfo', 'showLengthMenu', 'total'];
TablePagination.propTypes = {
  lengthMenu: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    value: _propTypes2.default.number.isRequired,
    label: _propTypes2.default.node.isRequired
  })),
  showLengthMenu: _propTypes2.default.bool,
  showInfo: _propTypes2.default.bool,
  total: _propTypes2.default.number.isRequired,
  displayLength: _propTypes2.default.number.isRequired,
  renderLengthMenu: _propTypes2.default.func,
  renderTotal: _propTypes2.default.func,
  onChangePage: _propTypes2.default.func,
  onChangeLength: _propTypes2.default.func,
  prev: _propTypes2.default.bool,
  next: _propTypes2.default.bool,
  first: _propTypes2.default.bool,
  last: _propTypes2.default.bool,
  maxButtons: _propTypes2.default.number,
  activePage: _propTypes2.default.number.isRequired,
  className: _propTypes2.default.string,
  locale: _propTypes2.default.shape({
    lengthMenuInfo: _propTypes2.default.string.isRequired,
    totalInfo: _propTypes2.default.string.isRequired
  }).isRequired,
  classPrefix: _propTypes2.default.string.isRequired
};
exports.default = (0, _compose2.default)((0, _withLocale2.default)(['TablePagination']), (0, _utils.defaultProps)({
  classPrefix: 'table-pagination'
}))(TablePagination);