"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Pagination", {
    enumerable: true,
    get: function() {
        return Pagination;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _configprovider = require("../configprovider");
var _usepropsvalue = require("../../hooks/use-props-value");
var _typings = require("../../utils/typings");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    defaultValue: 1,
    mode: 'multi',
    prev: null,
    next: null,
    total: 50,
    pageSize: 10,
    itemSize: 5,
    ellipse: false
});
var Pagination = function Pagination(props) {
    var locale = (0, _configprovider.useConfig)().locale;
    var _ref = (0, _object_spread._)({}, defaultProps, props), value = _ref.value, mode = _ref.mode, prev = _ref.prev, next = _ref.next, total = _ref.total, pageSize = _ref.pageSize, itemSize = _ref.itemSize, onChange = _ref.onChange, ellipse = _ref.ellipse, itemRender = _ref.itemRender, defaultValue = _ref.defaultValue, className = _ref.className, style = _ref.style, rest = (0, _object_without_properties._)(_ref, [
        "value",
        "mode",
        "prev",
        "next",
        "total",
        "pageSize",
        "itemSize",
        "onChange",
        "ellipse",
        "itemRender",
        "defaultValue",
        "className",
        "style"
    ]);
    var classPrefix = 'nut-pagination';
    var _usePropsValue = (0, _sliced_to_array._)((0, _usepropsvalue.usePropsValue)({
        value: value,
        defaultValue: defaultValue,
        finalValue: 1,
        onChange: onChange
    }), 2), currentPage = _usePropsValue[0], setCurrentPage = _usePropsValue[1];
    // (total + pageSize) => pageCount 计算页面的数量
    var pageCount = (0, _react.useMemo)(function() {
        var num = Math.ceil(total / pageSize);
        return Number.isNaN(num) ? 1 : Math.max(1, num);
    }, [
        total,
        pageSize
    ]);
    // (currentPage + itemSize + pageCount) => pages 显示的 item 列表
    var pages = (0, _react.useMemo)(function() {
        var items = [];
        var startPage = 1;
        var endPage = pageCount;
        var partialShow = pageCount > itemSize;
        if (partialShow) {
            // 选中的 page 放在中间位置
            startPage = Math.max(currentPage - Math.floor(itemSize / 2), 1);
            endPage = startPage + itemSize - 1;
            if (endPage > pageCount) {
                endPage = pageCount;
                startPage = endPage - itemSize + 1;
            }
        }
        // 遍历生成数组
        for(var i = startPage; i <= endPage; i++){
            items.push({
                number: i,
                text: i
            });
        }
        // 判断是否有折叠
        if (partialShow && itemSize > 0 && ellipse) {
            if (startPage > 1) {
                items.unshift({
                    number: startPage - 1,
                    text: '...'
                });
            }
            if (endPage < pageCount) {
                items.push({
                    number: endPage + 1,
                    text: '...'
                });
            }
        }
        return items;
    }, [
        currentPage,
        itemSize,
        pageCount
    ]);
    var handleSelectPage = function handleSelectPage(curPage) {
        if (curPage > pageCount || curPage < 1) return;
        setCurrentPage(curPage);
    };
    return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        className: (0, _classnames.default)(classPrefix, className)
    }, rest), (mode === 'multi' || mode === 'simple') && /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(classPrefix, "-prev"), mode === 'multi' ? '' : "".concat(classPrefix, "-simple-border"), currentPage === 1 ? "".concat(classPrefix, "-prev-disabled") : ''),
        onClick: function onClick(e) {
            return handleSelectPage(currentPage - 1);
        }
    }, prev || locale.pagination.prev), mode === 'multi' && /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-contain")
    }, pages.map(function(item, index) {
        return /*#__PURE__*/ _react.default.createElement("div", {
            key: "".concat(index, "pagination"),
            className: (0, _classnames.default)("".concat(classPrefix, "-item"), (0, _define_property._)({}, "".concat(classPrefix, "-item-active"), item.number === currentPage)),
            onClick: function onClick(e) {
                item.number !== currentPage && handleSelectPage(item.number);
            }
        }, itemRender ? itemRender(item, currentPage) : item.text);
    })), mode === 'simple' && /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-contain")
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-simple")
    }, currentPage, "/", pageCount)), /*#__PURE__*/ _react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(classPrefix, "-next"), currentPage >= pageCount ? "".concat(classPrefix, "-next-disabled") : ''),
        onClick: function onClick(e) {
            return handleSelectPage(currentPage + 1);
        }
    }, next || locale.pagination.next)), mode === 'lite' && /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-lite")
    }, /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-lite-active")
    }, currentPage), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-lite-spliterator")
    }, "/"), /*#__PURE__*/ _react.default.createElement("div", {
        className: "".concat(classPrefix, "-lite-default")
    }, pageCount)));
};
Pagination.displayName = 'NutPagination';
