"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.Pagination = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _lodash = _interopRequireDefault(require("lodash"));

var _PurePagination = require("../PurePagination");

var Pagination =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(Pagination, _React$Component);

  function Pagination(props) {
    var _this;

    _this = _React$Component.call(this, props) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChangePageSize", function (value, selectedIndex) {
      var eventData = {
        value: value,
        selectedIndex: selectedIndex
      };

      if (typeof _this.props.beforeDropdownChange === 'function') {
        _this.props.beforeDropdownChange(_this.dropdownChange.bind((0, _assertThisInitialized2.default)(_this), eventData));
      } else {
        _this.dropdownChange(eventData);
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleChangePage", function (page) {
      _this.navigate(page);
    });
    _this.store = _this.props.store;
    _this.state = _this.getOriginalState();

    _this.onStoreChange = function () {
      if (_this.unmounted) {
        return;
      }

      _this.setState({
        itemsCount: _this.store.getFilteredCount(),
        activePage: _this.store.getCurrentPage(),
        pageSize: _this.store.getPageSize()
      });
    };

    return _this;
  }

  var _proto = Pagination.prototype;

  _proto.componentWillMount = function componentWillMount() {
    this.store.on('change', this.onStoreChange, this);
  };

  _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    this.store.off('change', this.onStoreChange);
    this.store = nextProps.store;
    this.setState(this.getOriginalState());
    this.store.on('change', this.onStoreChange, this);
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    this.unmounted = true;
    this.store.off('change', this.onStoreChange);
  };

  _proto.getOriginalState = function getOriginalState() {
    var isPageSizeCollision = !_lodash.default.includes(this.props.availablePageSizes, this.store.getPageSize());
    var pageSize = isPageSizeCollision ? this.props.defaultPageSize : this.store.getPageSize();

    if (isPageSizeCollision) {
      this.store.setPageSize(this.props.defaultPageSize);
    }

    return {
      itemsCount: this.store.getFilteredCount(),
      activePage: this.store.getCurrentPage(),
      pageSize: pageSize
    };
  };

  _proto.navigate = function navigate(page) {
    var beforeNavigate = this.props.beforeNavigate;

    if (typeof beforeNavigate === 'function') {
      beforeNavigate(this.handleNavigate.bind(this, page));
    } else {
      this.handleNavigate(page);
    }
  };

  _proto.dropdownChange = function dropdownChange(_ref) {
    var pageSize = _ref.value,
        selectedIndex = _ref.selectedIndex;
    var _this$props = this.props,
        onPageSizeChange = _this$props.onPageSizeChange,
        onPageChange = _this$props.onPageChange;
    var actualIndex = selectedIndex;
    var prevActivePage = this.state.activePage;
    var nextActivePage = 0;
    this.setState({
      pageSize: pageSize,
      activePage: nextActivePage
    });
    this.store.setPageSize(pageSize);

    if (typeof onPageSizeChange === 'function') {
      onPageSizeChange(pageSize, actualIndex);
    }

    if (prevActivePage !== nextActivePage && typeof onPageChange === 'function') {
      onPageChange(nextActivePage);
    }
  };

  _proto.handleNavigate = function handleNavigate(activePage) {
    var onPageChange = this.props.onPageChange;
    this.setState({
      activePage: activePage
    });
    this.store.setCurrentPage(activePage);

    if (typeof onPageChange === 'function') {
      onPageChange(activePage);
    }
  };

  _proto.render = function render() {
    return _react.default.createElement(_PurePagination.PurePagination, (0, _extends2.default)({}, this.props, {
      activePage: this.state.activePage,
      itemsCount: this.state.itemsCount,
      pageSize: this.state.pageSize,
      pageSizes: this.props.availablePageSizes,
      onChangePage: this.handleChangePage,
      onChangePageSize: this.handleChangePageSize
    }));
  };

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

exports.Pagination = Pagination;
(0, _defineProperty2.default)(Pagination, "PagingModes", _PurePagination.PurePagination.PagingModes);
(0, _defineProperty2.default)(Pagination, "propTypes", {
  store: _propTypes.default.object.isRequired,
  availablePageSizes: _propTypes.default.array,
  defaultPageSize: _propTypes.default.number,
  beforeNavigate: _propTypes.default.func,
  beforeDropdownChange: _propTypes.default.func,
  onPageSizeChange: _propTypes.default.func,
  onPageChange: _propTypes.default.func
});
(0, _defineProperty2.default)(Pagination, "defaultProps", {
  availablePageSizes: [10, 25, 50, 100, 500],
  defaultPageSize: 25,
  beforeNavigate: null,
  beforeDropdownChange: null,
  onPageSizeChange: null,
  onPageChange: null
});