var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';
import { PurePagination } from '../PurePagination';
var Pagination = /** @class */ (function (_super) {
    __extends(Pagination, _super);
    function Pagination(props) {
        var _this = _super.call(this, props) || this;
        _this.handleChangePageSize = function (value, selectedIndex) {
            var eventData = { value: value, selectedIndex: selectedIndex };
            if (typeof _this.props.beforeDropdownChange === 'function') {
                _this.props.beforeDropdownChange(_this.dropdownChange.bind(_this, eventData));
            }
            else {
                _this.dropdownChange(eventData);
            }
        };
        _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;
    }
    Pagination.prototype.componentWillMount = function () {
        this.store.on('change', this.onStoreChange, this);
    };
    Pagination.prototype.componentWillReceiveProps = function (nextProps) {
        this.store.off('change', this.onStoreChange);
        this.store = nextProps.store;
        this.setState(this.getOriginalState());
        this.store.on('change', this.onStoreChange, this);
    };
    Pagination.prototype.componentWillUnmount = function () {
        this.unmounted = true;
        this.store.off('change', this.onStoreChange);
    };
    Pagination.prototype.getOriginalState = function () {
        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
        };
    };
    Pagination.prototype.navigate = function (page) {
        var beforeNavigate = this.props.beforeNavigate;
        if (typeof beforeNavigate === 'function') {
            beforeNavigate(this.handleNavigate.bind(this, page));
        }
        else {
            this.handleNavigate(page);
        }
    };
    Pagination.prototype.dropdownChange = function (_a) {
        var pageSize = _a.value, selectedIndex = _a.selectedIndex;
        var _b = this.props, onPageSizeChange = _b.onPageSizeChange, onPageChange = _b.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);
        }
    };
    Pagination.prototype.handleNavigate = function (activePage) {
        var onPageChange = this.props.onPageChange;
        this.setState({ activePage: activePage });
        this.store.setCurrentPage(activePage);
        if (typeof onPageChange === 'function') {
            onPageChange(activePage);
        }
    };
    Pagination.prototype.render = function () {
        return (React.createElement(PurePagination, __assign({}, this.props, { activePage: this.state.activePage, itemsCount: this.state.itemsCount, pageSize: this.state.pageSize, pageSizes: this.props.availablePageSizes, onChangePage: this.handleChangePage, onChangePageSize: this.handleChangePageSize })));
    };
    Pagination.PagingModes = PurePagination.PagingModes;
    Pagination.propTypes = {
        store: PropTypes.object.isRequired,
        availablePageSizes: PropTypes.array,
        defaultPageSize: PropTypes.number,
        beforeNavigate: PropTypes.func,
        beforeDropdownChange: PropTypes.func,
        onPageSizeChange: PropTypes.func,
        onPageChange: PropTypes.func
    };
    Pagination.defaultProps = {
        availablePageSizes: [10, 25, 50, 100, 500],
        defaultPageSize: 25,
        beforeNavigate: null,
        beforeDropdownChange: null,
        onPageSizeChange: null,
        onPageChange: null
    };
    return Pagination;
}(React.Component));
export { Pagination };
//# sourceMappingURL=Pagination.js.map