import React from 'react';
import { Button, Icons } from '@storybook/components';
import { noop } from '../../../../types';
import { styled } from '@storybook/theming';
const StyledButton = styled(Button)({
  transform: 'none',
  marginLeft: '8px'
});
export function Paging(props) {
  const renderItem = (item, index) => {
    switch (item) {
      case '.':
        {
          return /*#__PURE__*/React.createElement(StyledButton, {
            disabled: true,
            key: `dots${index < 5 ? 'Left' : 'Right'}`
          }, '...');
        }
      case 'forward':
        {
          const disabled = props.activePage === props.pagesCount;
          return /*#__PURE__*/React.createElement(StyledButton, {
            outline: true,
            disabled: disabled,
            onClick: disabled ? noop : () => goToPage(props.activePage + 1),
            key: "forward"
          }, /*#__PURE__*/React.createElement("span", null, "Next ", /*#__PURE__*/React.createElement(Icons, {
            icon: "arrowright"
          })));
        }
      default:
        {
          return /*#__PURE__*/React.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.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');
}