import { __extends } from "tslib";
import { Component } from 'react';
import memoize from '../../utils/memorize-one';
import { hasOwnProperty } from '../../utils/hasOwn';
var BasePagination = (function (_super) {
    __extends(BasePagination, _super);
    function BasePagination() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getLayout = memoize(function (props) {
            return _this.layoutFn(_this.getLayoutOptions(props));
        });
        _this.onPageChange = function (page) {
            var _a = _this.props, current = _a.current, pageSize = _a.pageSize, onChange = _a.onChange;
            var total = _this.getTotal();
            page = Math.max(1, page);
            page = Math.min(page, _this.getTotalPages(total, pageSize));
            if (page !== current) {
                onChange({
                    current: page,
                    pageSize: pageSize,
                });
            }
        };
        _this.onPageSizeChange = function (pageSize) {
            var current = _this.props.current;
            var total = _this.getTotal();
            if (_this.props.pageSize !== pageSize) {
                var maxPageNumber = _this.getTotalPages(total, pageSize);
                var options = {
                    pageSize: pageSize,
                    current: Math.min(current, maxPageNumber),
                };
                _this.props.onChange(options);
            }
        };
        return _this;
    }
    BasePagination.prototype.shouldUpdateLayout = function (props, nextProps) {
        var current = nextProps.current, pageSize = nextProps.pageSize;
        return (current !== props.current ||
            this.getTotal(nextProps) !== this.getTotal(props) ||
            pageSize !== props.pageSize);
    };
    BasePagination.prototype.getLayoutOptions = function (props) {
        var current = props.current, pageSize = props.pageSize;
        return {
            current: current,
            total: this.getTotal(props),
            pageSize: pageSize,
        };
    };
    BasePagination.prototype.getTotalPages = function (total, pageSize) {
        return Math.ceil(total / pageSize);
    };
    BasePagination.prototype.getTotal = function (props) {
        props = props || this.props;
        if (hasOwnProperty(props, 'total')) {
            return props.total || 0;
        }
        if (hasOwnProperty(props, 'totalItem')) {
            return props.totalItem || 0;
        }
        return 0;
    };
    return BasePagination;
}(Component));
export { BasePagination };
export default BasePagination;
