import React, { Component, PropTypes } from 'react'
import range from 'lodash/range'

export default class Paginator extends Component {
  static propTypes = {
    initialPage: PropTypes.number,
    numberOfPages: PropTypes.number.isRequired,
    onPageChange: PropTypes.func,
    children: PropTypes.func.isRequired
  }

  static defaultProps = { initialPage: 1 }

  state = { page: this.props.initialPage }

  componentWillMount() {
    if (this.props.initialPage < 1) {
      throw new Error('initialPage should be greater than or equal to 1')
    }
    if (this.props.initialPage > this.props.numberOfPages) {
      throw new Error(
        `initialPage should be less than or equal to ${this.props.numberOfPages}`
      )
    }
  }

  getPageRange = (offset = 3) => {
    if (typeof offset !== 'number') {
      throw new Error('offset should be a number')
    }
    const start = this.state.page - offset
    const end = this.state.page + offset
    return range(
      start > 1 ? start : 1,
      (end < this.props.numberOfPages ? end : this.props.numberOfPages) + 1
    )
  }

  setPage = page => {
    if (typeof page !== 'number') {
      throw new Error('page should be a number')
    }
    if (page < 1) {
      throw new Error('page should be greater than or equal to 1')
    }
    if (page > this.props.numberOfPages) {
      throw new Error(
        `page should be less than or equal to ${this.props.numberOfPages}`
      )
    }
    this.setState({ page }, () => {
      if (this.props.onPageChange) {
        this.props.onPageChange(page)
      }
    })
  }

  incrementPage = () => {
    if (this.state.page === this.props.numberOfPages) {
      return
    }
    this.setPage(this.state.page + 1)
  }

  decrementPage = () => {
    if (this.state.page === 1) {
      return
    }
    this.setPage(this.state.page - 1)
  }

  render() {
    return this.props.children({
      ...this.state,
      getPageRange: this.getPageRange,
      setPage: this.setPage,
      incrementPage: this.incrementPage,
      decrementPage: this.decrementPage
    })
  }
}

@@ -179,285 +179,151 @@
 ge: 
-PropTypes.number,%0D%0A    numberOfPages: PropTypes.number.isRequired,%0D%0A    onPageChange: PropTypes.func,
+props =%3E %7B%0D%0A      if (typeof props.initialPage !== 'number') %7B
 %0D%0A    
-children: PropTypes.func.isRequired%0D%0A  %7D%0D%0A%0D%0A  static defaultProps = %7B initialPage: 1 %7D%0D%0A%0D%0A  state = %7B page: this.props.initialPage %7D%0D%0A%0D%0A  componentWillMount() %7B%0D%0A
+    return new Error(%60initialPage should be a number%60)%0D%0A      %7D%0D%0A  
     if (
 this
@@ -310,37 +310,32 @@
    %7D%0D%0A      if (
-this.
 props.initialPag
@@ -336,32 +336,34 @@
 ialPage %3C 1) %7B%0D%0A
+  
       throw new 
@@ -425,27 +425,31 @@
 ')%0D%0A    
+  
 %7D%0D%0A
+  
     if (
 this.pro
@@ -436,29 +436,24 @@
 %0D%0A      if (
-this.
 props.initia
@@ -456,29 +456,24 @@
 itialPage %3E 
-this.
 props.number
@@ -482,32 +482,34 @@
 Pages) %7B%0D%0A      
+  
 throw new Error(
@@ -502,32 +502,34 @@
 row new Error(%0D%0A
+  
         %60initial
@@ -559,37 +559,32 @@
 n or equal to $%7B
-this.
 props.numberOfPa
@@ -596,28 +596,257 @@
 %0D%0A      
+  
 )%0D%0A    
+  
 %7D%0D%0A  
+  %7D,%0D%0A    numberOfPages: PropTypes.number.isRequired,%0D%0A    onPageChange: PropTypes.func,%0D%0A    children: PropTypes.func.isRequired%0D%0A  %7D%0D%0A%0D%0A  static defaultProps = %7B initialPage: 1 %7D%0D%0A%0D%0A  state = %7B page: this.props.initialPage 
 %7D%0D%0A%0D%0A  g

Improve Paginator validation