import _extends from "@babel/runtime/helpers/extends";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { PurePagination } from '../PurePagination';
export var Pagination =
/*#__PURE__*/
function (_React$Component) {
  _inheritsLoose(Pagination, _React$Component);

  function Pagination(props) {
    var _this;

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

    _defineProperty(_assertThisInitialized(_this), "handleChangePageSize", function (value, selectedIndex) {
      var eventData = {
        value: value,
        selectedIndex: selectedIndex
      };

      if (typeof _this.props.beforeDropdownChange === 'function') {
        _this.props.beforeDropdownChange(_this.dropdownChange.bind(_assertThisInitialized(_this), eventData));
      } else {
        _this.dropdownChange(eventData);
      }
    });

    _defineProperty(_assertThisInitialized(_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 = !_.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.createElement(PurePagination, _extends({}, 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.Component);

_defineProperty(Pagination, "PagingModes", PurePagination.PagingModes);

_defineProperty(Pagination, "propTypes", {
  store: PropTypes.object.isRequired,
  availablePageSizes: PropTypes.array,
  defaultPageSize: PropTypes.number,
  beforeNavigate: PropTypes.func,
  beforeDropdownChange: PropTypes.func,
  onPageSizeChange: PropTypes.func,
  onPageChange: PropTypes.func
});

_defineProperty(Pagination, "defaultProps", {
  availablePageSizes: [10, 25, 50, 100, 500],
  defaultPageSize: 25,
  beforeNavigate: null,
  beforeDropdownChange: null,
  onPageSizeChange: null,
  onPageChange: null
});