"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _constant = require("../utils/constant");

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function makePage(number, text, active) {
  return {
    number: number,
    text: text,
    active: active
  };
}

var Pagination = function Pagination(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

  var _createNamespace = createNamespace('pagination', prefixCls),
      bem = _createNamespace[0];

  var count = (0, _react.useMemo)(function () {
    var pageCount = props.pageCount,
        totalItems = props.totalItems,
        itemsPerPage = props.itemsPerPage;
    var innerCount = +pageCount || Math.ceil(+totalItems / +itemsPerPage);
    return Math.max(1, innerCount);
  }, [props.pageCount, props.totalItems, props.itemsPerPage]);
  var pages = (0, _react.useMemo)(function () {
    var items = [];
    var pageCount = count;
    var showPageSize = +props.showPageSize;
    var value = props.value,
        forceEllipses = props.forceEllipses;

    if (props.mode !== 'multi') {
      return items;
    } // Default page limits


    var startPage = 1;
    var endPage = pageCount;
    var isMaxSized = showPageSize < pageCount; // recompute if showPageSize

    if (isMaxSized) {
      // Current page is displayed in the middle of the visible ones
      startPage = Math.max(value - Math.floor(showPageSize / 2), 1);
      endPage = startPage + showPageSize - 1; // Adjust if limit is exceeded

      if (endPage > pageCount) {
        endPage = pageCount;
        startPage = endPage - showPageSize + 1;
      }
    } // Add page number links
    // eslint-disable-next-line no-plusplus


    for (var number = startPage; number <= endPage; number++) {
      var page = makePage(number, number, number === value);
      items.push(page);
    } // Add links to move between page sets


    if (isMaxSized && showPageSize > 0 && forceEllipses) {
      if (startPage > 1) {
        var prevPages = makePage(startPage - 1, '...');
        items.unshift(prevPages);
      }

      if (endPage < pageCount) {
        var nextPages = makePage(endPage + 1, '...');
        items.push(nextPages);
      }
    }

    return items;
  }, [props.showPageSize, props.forceEllipses, props.value]);

  var select = function select(page, emitChange) {
    page = Math.min(count, Math.max(1, page));

    if (props.value !== page) {
      // emit('update:modelValue', page);
      if (emitChange) {
        props.onChange == null ? void 0 : props.onChange(page);
      }
    }
  };

  (0, _react.useEffect)(function () {
    select(props.value);
  }, [props.value]);

  var renderDesc = function renderDesc() {
    if (props.mode !== 'multi') {
      return /*#__PURE__*/_react.default.createElement("li", {
        className: (0, _clsx.default)(bem('page-desc'))
      }, props.pageDesc ? props.pageDesc : props.value + "/" + count);
    }

    return null;
  };

  var simple = props.mode !== 'multi';

  var onSelect = function onSelect(value) {
    return function () {
      return select(value, true);
    };
  };

  var value = props.value;
  return /*#__PURE__*/_react.default.createElement("ul", {
    className: (0, _clsx.default)(bem({
      simple: simple
    }))
  }, /*#__PURE__*/_react.default.createElement("li", {
    className: (0, _clsx.default)(bem('item', {
      disabled: value === 1
    }), bem('prev'), _constant.BORDER),
    onClick: onSelect(value - 1)
  }, props.prevText || locale.vanPagination.prev), pages.map(function (page, index) {
    return /*#__PURE__*/_react.default.createElement("li", {
      // eslint-disable-next-line react/no-array-index-key
      key: index,
      className: (0, _clsx.default)(bem('item', {
        active: page.active
      }), bem('page'), _constant.BORDER),
      onClick: onSelect(page.number)
    }, props.pageRender ? props.pageRender(page) : page.text);
  }), renderDesc(), /*#__PURE__*/_react.default.createElement("li", {
    className: (0, _clsx.default)(bem('item', {
      disabled: value === count
    }), bem('next'), _constant.BORDER),
    onClick: onSelect(value + 1)
  }, props.nextText || locale.vanPagination.next));
};

Pagination.defaultProps = {
  mode: 'multi',
  pageCount: 0,
  totalItems: 0,
  itemsPerPage: 10,
  showPageSize: 5
};
var _default = Pagination;
exports.default = _default;