import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { 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,
    text,
    active
  };
}

const Pagination = props => {
  const {
    prefixCls,
    createNamespace,
    locale
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('pagination', prefixCls);
  const count = useMemo(() => {
    const {
      pageCount,
      totalItems,
      itemsPerPage
    } = props;
    const innerCount = +pageCount || Math.ceil(+totalItems / +itemsPerPage);
    return Math.max(1, innerCount);
  }, [props.pageCount, props.totalItems, props.itemsPerPage]);
  const pages = useMemo(() => {
    const items = [];
    const pageCount = count;
    const showPageSize = +props.showPageSize;
    const {
      value,
      forceEllipses
    } = props;

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


    let startPage = 1;
    let endPage = pageCount;
    const 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 (let number = startPage; number <= endPage; number++) {
      const page = makePage(number, number, number === value);
      items.push(page);
    } // Add links to move between page sets


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

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

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

  const select = (page, emitChange) => {
    var _a;

    page = Math.min(count, Math.max(1, page));

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

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

  const renderDesc = () => {
    if (props.mode !== 'multi') {
      return _jsx("li", Object.assign({
        className: clsx(bem('page-desc'))
      }, {
        children: props.pageDesc ? props.pageDesc : `${props.value}/${count}`
      }));
    }

    return null;
  };

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

  const onSelect = value => () => select(value, true);

  const {
    value
  } = props;
  return _jsxs("ul", Object.assign({
    className: clsx(bem({
      simple
    }))
  }, {
    children: [_jsx("li", Object.assign({
      className: clsx(bem('item', {
        disabled: value === 1
      }), bem('prev'), BORDER),
      onClick: onSelect(value - 1)
    }, {
      children: props.prevText || locale.vanPagination.prev
    })), pages.map((page, index) => _jsx("li", Object.assign({
      className: clsx(bem('item', {
        active: page.active
      }), bem('page'), BORDER),
      onClick: onSelect(page.number)
    }, {
      children: props.pageRender ? props.pageRender(page) : page.text
    }), index)), renderDesc(), _jsx("li", Object.assign({
      className: clsx(bem('item', {
        disabled: value === count
      }), bem('next'), BORDER),
      onClick: onSelect(value + 1)
    }, {
      children: props.nextText || locale.vanPagination.next
    }))]
  }));
};

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