var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import ConfigProvider from '../config-provider';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import cls from 'classnames';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { Input } from '../index';
import { Pagination as NextPagination } from '@alifd/next';
import { getTheme } from '../utils/getTheme';
class Pagination extends Component {
    constructor(props) {
        super(props);
        this.onInputChange = value => {
            this.setState({
                inputValue: value,
            });
        };
        this.handleJump = e => {
            const { currentPage, inputValue } = this.state;
            const { total, pageSize } = this.state;
            let value = parseInt(String(inputValue), 10);
            const totalPage = Math.ceil(total / pageSize) <= 0 ? 1 : Math.ceil(total / pageSize);
            if (isNaN(value)) {
                value = currentPage;
            }
            else if (value < 1) {
                value = 1;
            }
            else if (value > totalPage) {
                value = totalPage;
            }
            if (value && value !== currentPage) {
                this.onChange(value, e);
            }
        };
        this.onKeyDown = e => {
            if (e.keyCode === 13) {
                this.handleJump(e);
            }
        };
        this.pageNumberRender = index => {
            const { inputValue } = this.state;
            const { prefix = 'next-' } = this.props;
            if (index === this.state.currentPage) {
                return (React.createElement("div", { className: `${prefix}pagonation-bdesign-page` },
                    React.createElement(Input, { size: 'xs', className: `${prefix}pagonation-current-input`, value: inputValue, onChange: this.onInputChange, onKeyDown: this.onKeyDown }),
                    React.createElement("div", { className: `${prefix}pagonation-lastPage-text` }, index)));
            }
            return index;
        };
        this.onChange = (current, e) => {
            const { onChange } = this.props;
            this.setState({
                currentPage: current,
                inputValue: current,
            });
            onChange && onChange(current, e);
        };
        this.state = {
            currentPage: this.props.current || this.props.defaultCurrent || 1,
            total: this.props.total || 100,
            pageSize: this.props.pageSize || 10,
            inputValue: this.props.current || this.props.defaultCurrent || 1,
        };
    }
    render() {
        const { prefix = 'next-', type, className } = this.props;
        const theme = getTheme(this.context, this.props);
        // mini 模式
        if (type === 'mini') {
            const _a = this.props, { prefix = 'next-', className } = _a, otherProps = __rest(_a, ["prefix", "className"]);
            return (React.createElement(NextPagination, Object.assign({}, otherProps, { type: "normal", showJump: false, prefix: prefix, size: "small", shape: "arrow-only", className: cls(className, {
                    [`${prefix}grey`]: theme === 'grey',
                    [`${prefix}white`]: theme === 'white',
                }) })));
        }
        if (type === 'simple') {
            const _b = this.props, { prefix = 'next-', className } = _b, otherProps = __rest(_b, ["prefix", "className"]);
            return (React.createElement(NextPagination, Object.assign({}, otherProps, { type: "simple", showJump: false, prefix: prefix, size: "small", shape: "arrow-only", current: this.state.currentPage, className: cls(className, {
                    [`${prefix}grey`]: theme === 'grey',
                    [`${prefix}white`]: theme === 'white',
                }), onChange: this.onChange, pageNumberRender: this.pageNumberRender })));
        }
        return React.createElement(NextPagination, Object.assign({}, this.props, { shape: 'arrow-only', size: 'medium', className: cls(className, {
                [`${prefix}grey`]: theme === 'grey',
                [`${prefix}white`]: theme === 'white',
            }) }));
    }
}
// contextTypes
Pagination.contextTypes = {
    theme: PropTypes.string,
};
hoistNonReactStatics(Pagination, NextPagination);
export default ConfigProvider.config(Pagination);
