import { __assign, __rest } from "tslib";
import React, { useContext, useEffect, useState } from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { ArrowDown } from '../_utils/components';
var Pagination = function (props) {
    var _a;
    var _b = props.align, align = _b === void 0 ? 'center' : _b, _c = props.size, size = _c === void 0 ? 'md' : _c, _d = props.defaultCurrent, defaultCurrent = _d === void 0 ? 1 : _d, _e = props.defaultPageSize, defaultPageSize = _e === void 0 ? 10 : _e, _f = props.total, total = _f === void 0 ? 0 : _f, _g = props.disabled, disabled = _g === void 0 ? false : _g, onChange = props.onChange, className = props.className, customisedCls = props.prefixCls, otherProps = __rest(props, ["align", "size", "defaultCurrent", "defaultPageSize", "total", "disabled", "onChange", "className", "prefixCls"]);
    var configContext = useContext(ConfigContext);
    var prefixCls = getPrefixCls('pagination', configContext.prefixCls, customisedCls);
    var cls = classNames(prefixCls, className, (_a = {},
        _a[prefixCls + "_" + align] = align,
        _a[prefixCls + "_" + size] = size,
        _a[prefixCls + "_disabled"] = disabled,
        _a));
    var _h = useState('current' in props ? props.current : defaultCurrent), current = _h[0], setCurrent = _h[1];
    var _j = useState('pageSize' in props ? props.pageSize : defaultPageSize), pageSize = _j[0], setPageSize = _j[1];
    var itemOnClick = function (item) {
        if (item.active || item.disabled) {
            return;
        }
        var count = Math.ceil(total / pageSize);
        var curr = 0;
        if (item.type === 'page') {
            curr = Number(item.label);
        }
        if (item.type === 'prev') {
            curr = current - 1 > 0 ? current - 1 : 1;
        }
        if (item.type === 'next') {
            curr = current + 1 <= count ? current + 1 : count;
        }
        if (item.type === 'jump-prev' || item.type === 'jump-next') {
            curr = item.type === 'jump-prev' ? current - 5 : current + 5;
            if (curr > count) {
                curr = count;
            }
            if (curr < 1) {
                curr = 1;
            }
        }
        !('current' in props) && setCurrent(curr);
        onChange && onChange(curr, total, pageSize);
    };
    var getItems = function () {
        var count = Math.ceil(total / pageSize);
        var items = [
            { type: 'prev', label: '<', disabled: disabled || current === 1 },
        ];
        var itemCount = count <= 5 ? count : 5;
        var num = 0;
        var basic = 0;
        if (current > 3 && count > 5) {
            items.push({ disabled: disabled, type: 'page', label: '1' });
        }
        if (current > 4 && count > 6) {
            items.push({ disabled: disabled, type: 'jump-prev', label: '•••' });
        }
        while (num < itemCount) {
            num += 1;
            if (current > 3 && count > 5) {
                basic = current - 3;
            }
            var label = num + basic;
            if (count - current === 0 && count > 5) {
                label -= 2;
            }
            if (count - current === 1 && count > 5) {
                label -= 1;
            }
            if (label <= count) {
                items.push({ disabled: disabled, type: 'page', label: "" + label, active: current === label });
            }
        }
        if (current + 3 < count && count > 6) {
            items.push({ disabled: disabled, type: 'jump-next', label: '•••' });
        }
        if (current + 2 < count && count > 5) {
            items.push({ disabled: disabled, type: 'page', label: "" + count });
        }
        items.push({ type: 'next', label: '>', disabled: disabled || current === count });
        return items;
    };
    var renderItem = function (item) {
        var type = item.type, label = item.label;
        switch (type) {
            case 'prev':
                return React.createElement(ArrowDown, { size: 12, className: prefixCls + "__left-arrow" });
            case 'next':
                return React.createElement(ArrowDown, { size: 12, className: prefixCls + "__right-arrow" });
            default:
                return label;
        }
    };
    useEffect(function () {
        'current' in props && setCurrent(props.current);
        'pageSize' in props && setPageSize(props.pageSize);
    }, [props]);
    return (React.createElement("ul", __assign({}, otherProps, { unselectable: "off", className: cls }), getItems().map(function (item, idx) {
        var _a;
        var active = item.active, disabled = item.disabled, type = item.type;
        var cls = classNames(prefixCls + "__item", (_a = {},
            _a[prefixCls + "__item_active"] = active,
            _a[prefixCls + "__item_disabled"] = disabled,
            _a[prefixCls + "__item_ellipsis"] = type === 'jump-next' || type === 'jump-prev',
            _a));
        return (React.createElement("li", { key: idx, className: cls, tabIndex: 0, onClick: function () { return itemOnClick(item); } }, renderItem(item)));
    })));
};
export default Pagination;
//# sourceMappingURL=index.js.map