import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useMemo } from "react";
import classNames from "classnames";
import { useConfig } from "../configprovider";
import { usePropsValue } from "../../hooks/use-props-value";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
    defaultValue: 1,
    mode: 'multi',
    prev: null,
    next: null,
    total: 50,
    pageSize: 10,
    itemSize: 5,
    ellipse: false
});
export var Pagination = function(props) {
    var locale = useConfig().locale;
    var _ref = _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 = _object_without_properties(_ref, [
        "value",
        "mode",
        "prev",
        "next",
        "total",
        "pageSize",
        "itemSize",
        "onChange",
        "ellipse",
        "itemRender",
        "defaultValue",
        "className",
        "style"
    ]);
    var classPrefix = 'nut-pagination';
    var _usePropsValue = _sliced_to_array(usePropsValue({
        value: value,
        defaultValue: defaultValue,
        finalValue: 1,
        onChange: onChange
    }), 2), currentPage = _usePropsValue[0], setCurrentPage = _usePropsValue[1];
    // (total + pageSize) => pageCount 计算页面的数量
    var pageCount = 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 = 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(curPage) {
        if (curPage > pageCount || curPage < 1) return;
        setCurrentPage(curPage);
    };
    return /*#__PURE__*/ React.createElement("div", _object_spread({
        className: classNames(classPrefix, className)
    }, rest), (mode === 'multi' || mode === 'simple') && /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(classPrefix, "-prev"), mode === 'multi' ? '' : "".concat(classPrefix, "-simple-border"), currentPage === 1 ? "".concat(classPrefix, "-prev-disabled") : ''),
        onClick: function(e) {
            return handleSelectPage(currentPage - 1);
        }
    }, prev || locale.pagination.prev), mode === 'multi' && /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-contain")
    }, pages.map(function(item, index) {
        return /*#__PURE__*/ React.createElement("div", {
            key: "".concat(index, "pagination"),
            className: classNames("".concat(classPrefix, "-item"), _define_property({}, "".concat(classPrefix, "-item-active"), item.number === currentPage)),
            onClick: function(e) {
                item.number !== currentPage && handleSelectPage(item.number);
            }
        }, itemRender ? itemRender(item, currentPage) : item.text);
    })), mode === 'simple' && /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-contain")
    }, /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-simple")
    }, currentPage, "/", pageCount)), /*#__PURE__*/ React.createElement("div", {
        className: classNames("".concat(classPrefix, "-next"), currentPage >= pageCount ? "".concat(classPrefix, "-next-disabled") : ''),
        onClick: function(e) {
            return handleSelectPage(currentPage + 1);
        }
    }, next || locale.pagination.next)), mode === 'lite' && /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-lite")
    }, /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-lite-active")
    }, currentPage), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-lite-spliterator")
    }, "/"), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(classPrefix, "-lite-default")
    }, pageCount)));
};
Pagination.displayName = 'NutPagination';
