"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Paging = Paging;
var _react = _interopRequireDefault(require("react"));
var _components = require("@storybook/components");
var _types = require("../../../../types");
var _theming = require("@storybook/theming");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const StyledButton = (0, _theming.styled)(_components.Button)({
  transform: 'none',
  marginLeft: '8px'
});
function Paging(props) {
  const renderItem = (item, index) => {
    switch (item) {
      case '.':
        {
          return /*#__PURE__*/_react.default.createElement(StyledButton, {
            disabled: true,
            key: `dots${index < 5 ? 'Left' : 'Right'}`
          }, '...');
        }
      case 'forward':
        {
          const disabled = props.activePage === props.pagesCount;
          return /*#__PURE__*/_react.default.createElement(StyledButton, {
            outline: true,
            disabled: disabled,
            onClick: disabled ? _types.noop : () => goToPage(props.activePage + 1),
            key: "forward"
          }, /*#__PURE__*/_react.default.createElement("span", null, "Next ", /*#__PURE__*/_react.default.createElement(_components.Icons, {
            icon: "arrowright"
          })));
        }
      default:
        {
          return /*#__PURE__*/_react.default.createElement(StyledButton, {
            outline: true,
            secondary: props.activePage === item,
            key: item,
            onClick: () => goToPage(item)
          }, item);
        }
    }
  };
  const goToPage = pageNumber => {
    if (1 <= pageNumber && pageNumber !== props.activePage && pageNumber <= props.pagesCount) {
      props.onPageChange(pageNumber);
    }
  };
  return /*#__PURE__*/_react.default.createElement("div", null, getItems(props.activePage, props.pagesCount).map(renderItem));
}
function getItems(active, total) {
  const result = [];
  const left = Math.max(Math.min(active - 2, total - 4), 1);
  const right = Math.min(Math.max(5, active + 2), total);
  const hasLeftDots = left > 3;
  const from = hasLeftDots ? left : 1;
  const hasRightDots = right < total - 2;
  const to = hasRightDots ? right : total;
  if (hasLeftDots) {
    result.push(1, '.');
  }
  for (let i = from; i <= to; ++i) {
    result.push(i);
  }
  if (hasRightDots) {
    result.push('.');
  }
  if (hasRightDots && isFinite(total)) {
    result.push(total);
  }
  return result.concat('forward');
}