var _excluded = ["initialPage", "name", "onChange", "pageSize", "total"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import { getKey } from '../../helpers';
import { usePageChange } from './usePageChange';
import { PAGINATION_BUTTON_VARIANT, PaginationButton } from './PaginationButton';
import { PaginationErrorMessage } from './PaginationErrorMessage';
import { StyledList } from './partials/StyledList';
import { StyledListItem } from './partials/StyledListItem';
import { StyledTextInput } from './partials/StyledTextInput';
import { StyledTotalPages } from './partials/StyledTotalPages';
import { useExternalId } from '../../hooks/useExternalId';
var KEY_PREFIX = 'pagination-item';
export var Pagination = function Pagination(_ref) {
  var _ref$initialPage = _ref.initialPage,
    initialPage = _ref$initialPage === void 0 ? 1 : _ref$initialPage,
    externalName = _ref.name,
    onChange = _ref.onChange,
    pageSize = _ref.pageSize,
    total = _ref.total,
    rest = _objectWithoutProperties(_ref, _excluded);
  var totalPages = Math.ceil(total / pageSize);
  var _usePageChange = usePageChange(initialPage, totalPages, onChange),
    currentPage = _usePageChange.currentPage,
    hasError = _usePageChange.hasError,
    onKeyDown = _usePageChange.onKeyDown,
    onPaginationButtonClickHandler = _usePageChange.onPaginationButtonClickHandler;
  var name = useExternalId('pagination', externalName);
  var isOnFirstPage = currentPage === 1;
  var isOnLastPage = currentPage === totalPages;
  return /*#__PURE__*/React.createElement(StyledList, rest, /*#__PURE__*/React.createElement(StyledListItem, {
    key: getKey(KEY_PREFIX, 'first')
  }, /*#__PURE__*/React.createElement(PaginationButton, {
    disabled: isOnFirstPage,
    onClick: onPaginationButtonClickHandler(1)
  }, PAGINATION_BUTTON_VARIANT.FIRST)), /*#__PURE__*/React.createElement(StyledListItem, {
    key: getKey(KEY_PREFIX, 'previous')
  }, /*#__PURE__*/React.createElement(PaginationButton, {
    disabled: isOnFirstPage,
    onClick: onPaginationButtonClickHandler(currentPage - 1)
  }, PAGINATION_BUTTON_VARIANT.PREV)), /*#__PURE__*/React.createElement(StyledListItem, null, hasError && /*#__PURE__*/React.createElement(PaginationErrorMessage, null), /*#__PURE__*/React.createElement(StyledTextInput, {
    label: "",
    "aria-label": "Enter page number",
    onKeyDown: onKeyDown,
    name: name,
    value: currentPage.toString()
  }), /*#__PURE__*/React.createElement(StyledTotalPages, null, "of ", totalPages)), /*#__PURE__*/React.createElement(StyledListItem, {
    key: getKey(KEY_PREFIX, 'next')
  }, /*#__PURE__*/React.createElement(PaginationButton, {
    disabled: isOnLastPage,
    onClick: onPaginationButtonClickHandler(currentPage + 1)
  }, PAGINATION_BUTTON_VARIANT.NEXT)), /*#__PURE__*/React.createElement(StyledListItem, {
    key: getKey(KEY_PREFIX, 'last')
  }, /*#__PURE__*/React.createElement(PaginationButton, {
    disabled: isOnLastPage,
    onClick: onPaginationButtonClickHandler(totalPages)
  }, PAGINATION_BUTTON_VARIANT.LAST)));
};
Pagination.displayName = 'Pagination';
// Pagination.whyDidYouRender = true