"use strict";
const React = require("react");
const pageItem = require("./page-item.js");
const useMergeValue = require("../_util/hooks/useMergeValue.js");
const pageOptions = require("./page-options.js");
const pageJumper = require("./page-jumper.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const pick = require("../_util/pick.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var _defaultCurrent = 1;
var _defaultPageSize = 10;
function getAllPages(pageSize, total) {
  return Math.ceil(total / pageSize);
}
function getBufferSize(bufferSize, allPages) {
  var min = 0;
  var max = Math.floor(allPages / 2) - 1;
  var newBufferSize = Math.max(bufferSize, min);
  return Math.min(newBufferSize, max);
}
function getAdjustPageSize(sizeOptions) {
  if (sizeOptions && sizeOptions.length) {
    return sizeOptions[0];
  }
  return _defaultPageSize;
}
var defaultProps = {
  total: 0,
  pageSizeChangeResetCurrent: true,
  bufferSize: 2
};
function Pagination(baseProps, ref) {
  var _a;
  var _b;
  var _c = React.useContext(index.ConfigContext), getPrefixCls = _c.getPrefixCls, ctxSize = _c.size, locale = _c.locale, componentConfig = _c.componentConfig, rtl = _c.rtl;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Pagination);
  var propTotal = props.total, propPageSize = props.pageSize, propCurrent = props.current, propShowMore = props.showMore, propSizeOptions = props.sizeOptions, pageSizeChangeResetCurrent = props.pageSizeChangeResetCurrent, defaultCurrent = props.defaultCurrent, defaultPageSize = props.defaultPageSize;
  var _d = __read(useMergeValue(_defaultCurrent, {
    defaultValue: defaultCurrent,
    value: propCurrent
  }), 2), current = _d[0], setCurrent = _d[1];
  var _e = __read(useMergeValue(getAdjustPageSize(propSizeOptions), {
    defaultValue: defaultPageSize,
    value: propPageSize
  }), 2), pageSize = _e[0], setPageSize = _e[1];
  var total = propTotal;
  var showMore = !!propShowMore;
  if (propCurrent && !props.onChange) {
    console.warn("Warning: you have provide current prop for pagination but without onChange handler , this will cause no-change when you change page. ");
  }
  function getAdjustedCurrent(newPageSize, newTotal) {
    var newAllPages = getAllPages(newPageSize, newTotal);
    var newCurrent = current > newAllPages ? newAllPages : current;
    return newCurrent;
  }
  React.useEffect(function() {
    var adjustPageSize = getAdjustPageSize(propSizeOptions);
    if (!("pageSize" in props)) {
      setPageSize(adjustPageSize);
    }
  }, [propSizeOptions]);
  React.useEffect(function() {
    var newCurrent = getAdjustedCurrent(pageSize, total);
    if (newCurrent !== current && !("current" in props)) {
      setCurrent(newCurrent);
    }
  }, [total, current, pageSize]);
  var onChange = function(pageNumber, size2) {
    if (pageNumber === void 0) {
      pageNumber = current;
    }
    if (size2 === void 0) {
      size2 = pageSize;
    }
    var onChange2 = props.onChange;
    onChange2 && onChange2(pageNumber, size2);
  };
  var onPageSizeChange = function(pageSize2) {
    var onPageSizeChange2 = props.onPageSizeChange;
    var allPages2 = getAllPages(pageSize2, total);
    var newState = {
      pageSize: pageSize2
    };
    if (pageSizeChangeResetCurrent) {
      newState.current = 1;
    } else {
      newState.current = current > allPages2 ? allPages2 : current;
    }
    if (!("pageSize" in props)) {
      setPageSize(newState.pageSize);
    }
    if (!("current" in props) && current !== newState.current) {
      setCurrent(newState.current);
    }
    onPageSizeChange2 && onPageSizeChange2(pageSize2, newState.current);
    onChange(pageSizeChangeResetCurrent ? 1 : newState.current, pageSize2);
  };
  var onPageNumberChange = function(pageNumber) {
    if (!("current" in props)) {
      setCurrent(pageNumber);
    }
    onChange(pageNumber);
  };
  var className = props.className, style = props.style, pageItemStyle = props.pageItemStyle, activePageItemStyle = props.activePageItemStyle, showTotal = props.showTotal, sizeCanChange = props.sizeCanChange, sizeOptions = props.sizeOptions, simple = props.simple, mini = props.mini, showJumper = props.showJumper, selectProps = props.selectProps, icons = props.icons, disabled = props.disabled, itemRender = props.itemRender, hideOnSinglePage = props.hideOnSinglePage;
  var size = props.size || ctxSize;
  var prefixCls = getPrefixCls("pagination");
  var innerSize = mini ? "mini" : size;
  var classNames$1 = classNames(prefixCls, prefixCls + "-size-" + innerSize, (_a = {}, _a[prefixCls + "-simple"] = simple, _a[prefixCls + "-disabled"] = disabled, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var renderPager;
  var pageList = [];
  var allPages = getAllPages(pageSize, total);
  var bufferSize = getBufferSize(props.bufferSize, allPages);
  if (hideOnSinglePage && allPages <= 1) {
    return null;
  }
  var pagerProps = {
    onClick: onPageNumberChange,
    rootPrefixCls: prefixCls,
    simple,
    current,
    allPages,
    icons,
    disabled,
    pageItemStyle,
    activePageItemStyle,
    itemRender
  };
  if (simple) {
    var prefix = prefixCls + "-item-simple";
    renderPager = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("ul", {
      className: prefixCls + "-list",
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.StepPager, {
        ...__assign({
          key: "previous"
        }, pagerProps, {
          type: pageItem.StepType.previous
        })
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("li", {
        className: prefix + "-pager",
        children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageJumper, {
          disabled,
          rootPrefixCls: prefixCls,
          totalPages: allPages,
          current,
          onPageChange: onPageNumberChange,
          simple: {
            showJumper: typeof showJumper === "boolean" ? showJumper : true
          },
          size: innerSize
        })
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.StepPager, {
        ...__assign({
          key: "next"
        }, pagerProps, {
          type: pageItem.StepType.next
        })
      })]
    });
  } else {
    var beginFoldPage = 1 + 2 + bufferSize;
    var endFoldPage = allPages - 2 - bufferSize;
    if (allPages <= 4 + bufferSize * 2 || current === beginFoldPage && current === endFoldPage) {
      for (var i = 1; i <= allPages; i++) {
        pageList.push(/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.default, {
          ...__assign({}, pagerProps, {
            key: i,
            pageNum: i
          })
        }));
      }
    } else {
      var left = 1;
      var right = allPages;
      var hasJumpPre = true;
      var hasJumpNext = true;
      if (current > beginFoldPage && current < endFoldPage) {
        right = current + bufferSize;
        left = current - bufferSize;
      } else if (current <= beginFoldPage) {
        hasJumpPre = false;
        left = 1;
        right = Math.max(beginFoldPage, bufferSize + current);
      } else if (current >= endFoldPage) {
        hasJumpNext = false;
        right = allPages;
        left = Math.min(endFoldPage, current - bufferSize);
      }
      for (var i = left; i <= right; i++) {
        pageList.push(/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.default, {
          ...__assign({
            key: i,
            pageNum: i
          }, pagerProps)
        }));
      }
      var JumpPre = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.JumpPager, {
        ...__assign({}, pagerProps, {
          key: left - 1,
          type: pageItem.StepType.previous,
          jumpPage: -(bufferSize * 2 + 1)
        })
      });
      var JumpNext = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.JumpPager, {
        ...__assign({}, pagerProps, {
          key: right + 1,
          type: pageItem.StepType.next,
          jumpPage: bufferSize * 2 + 1
        })
      });
      var FirstPager = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.default, {
        ...__assign({
          key: 1,
          pageNum: 1
        }, pagerProps)
      });
      var LastPager = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.default, {
        ...__assign({}, pagerProps, {
          key: allPages,
          pageNum: allPages
        })
      });
      if (hasJumpPre) {
        pageList[0] = React__default.default.cloneElement(pageList[0], {
          className: prefixCls + "-item-after-pre"
        });
        pageList.unshift(JumpPre);
        pageList.unshift(FirstPager);
      }
      if (hasJumpNext) {
        pageList[pageList.length - 1] = React__default.default.cloneElement(pageList[pageList.length - 1], {
          className: prefixCls + "-item-before-next"
        });
        pageList.push(JumpNext);
        pageList.push(LastPager);
      }
    }
    renderPager = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("ul", {
      className: prefixCls + "-list",
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.StepPager, {
        ...__assign({}, pagerProps, {
          key: "previous",
          type: pageItem.StepType.previous
        })
      }), pageList, showMore && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.JumpPager, {
        ...__assign({}, pagerProps, {
          key: allPages + 1,
          type: pageItem.StepType.next,
          jumpPage: bufferSize * 2 + 1
        })
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageItem.StepPager, {
        ...__assign({
          key: "next"
        }, pagerProps, {
          type: pageItem.StepType.next
        })
      })]
    });
  }
  var totalElement = null;
  if (typeof showTotal === "boolean" && showTotal) {
    totalElement = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-total-text",
      children: (_b = locale.Pagination.total) === null || _b === void 0 ? void 0 : _b.replace("{0}", total)
    });
  }
  if (typeof showTotal === "function") {
    totalElement = /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-total-text",
      children: showTotal(total, [(current - 1) * pageSize + 1, current * pageSize])
    });
  }
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
    ...__assign({}, pick.pickDataAttributes(props), {
      className: classNames$1,
      style,
      ref
    }),
    children: [totalElement, renderPager, /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageOptions, {
      disabled,
      rootPrefixCls: prefixCls,
      sizeCanChange,
      sizeOptions,
      onPageSizeChange,
      pageSize,
      size: innerSize,
      selectProps
    }), !simple && showJumper && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(pageJumper, {
      disabled,
      rootPrefixCls: prefixCls,
      totalPages: allPages,
      current,
      onPageChange: onPageNumberChange,
      size: innerSize
    })]
  });
}
var PaginationComponent = React.forwardRef(Pagination);
PaginationComponent.displayName = "Pagination";
const Pagination$1 = PaginationComponent;
module.exports = Pagination$1;
//# sourceMappingURL=pagination.js.map
