import React, { useContext, useEffect, useMemo } from 'react';
import clsx from 'clsx';
import { BORDER } from '../utils/constant';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';

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

var Pagination = function Pagination(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

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

  var count = 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 = 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);
      }
    }
  };

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

  var renderDesc = function renderDesc() {
    if (props.mode !== 'multi') {
      return /*#__PURE__*/React.createElement("li", {
        className: clsx(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.createElement("ul", {
    className: clsx(bem({
      simple: simple
    }))
  }, /*#__PURE__*/React.createElement("li", {
    className: clsx(bem('item', {
      disabled: value === 1
    }), bem('prev'), BORDER),
    onClick: onSelect(value - 1)
  }, props.prevText || locale.vanPagination.prev), pages.map(function (page, index) {
    return /*#__PURE__*/React.createElement("li", {
      // eslint-disable-next-line react/no-array-index-key
      key: index,
      className: clsx(bem('item', {
        active: page.active
      }), bem('page'), BORDER),
      onClick: onSelect(page.number)
    }, props.pageRender ? props.pageRender(page) : page.text);
  }), renderDesc(), /*#__PURE__*/React.createElement("li", {
    className: clsx(bem('item', {
      disabled: value === count
    }), bem('next'), BORDER),
    onClick: onSelect(value + 1)
  }, props.nextText || locale.vanPagination.next));
};

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