/* istanbul ignore file */

import React from 'react';
import PropTypes from 'prop-types';
import { connectPagination } from 'react-instantsearch-dom';

// Style
let classes = {};
/* istanbul ignore next */
if (typeof window !== 'undefined') classes = require('./style.module.scss');

const CustomPagination = ({ currentRefinement, nbPages, refine }) => {
  if (nbPages <= 1) return null;

  const handlePageChange = (e, i) => {
    refine(currentRefinement + i);
    e.stopPropagation();
  };

  return (
    <footer className={classes.SearchPagination}>
      {currentRefinement !== nbPages && (
        <button
          className={classes['SearchPagination-Button']}
          focus-hit="firstElementChild"
          onClick={e => handlePageChange(e, 1)}
          type="button"
        >
          Next
        </button>
      )}
      <span className={classes['SearchPagination-State']}>
        {currentRefinement} of {nbPages}
      </span>
      {currentRefinement !== 1 && (
        <button
          className={classes['SearchPagination-Button']}
          focus-hit="lastElementChild"
          onClick={e => handlePageChange(e, -1)}
          type="button"
        >
          Back
        </button>
      )}
    </footer>
  );
};

const Pagination = connectPagination(CustomPagination);

CustomPagination.propTypes = {
  currentRefinement: PropTypes.number,
  nbPages: PropTypes.number,
  refine: PropTypes.func,
};

export default Pagination;
