import warning from 'warning'
import React, { PropTypes } from 'react'
import History from './History'
import MatchCountProvider from './MatchCountProvider'
import {
  history as historyType,
  location as locationType,
  router as routerType
} from './PropTypes'

class Router extends React.Component {
  static propTypes = {
    history: historyType,
    location: locationType,
    children: PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]),
    createHref: PropTypes.func,
    onPush: PropTypes.func,
    onReplace: PropTypes.func
  }

  static defaultProps = {
    createHref(to) {
      return this.props.history.createHref(to)
    },
    onPush(location) {
      if (this.props.history) {
        this.props.history.push(location)
      } else {
        warning(
          false,
          'Missing <Router history> prop; <Link>s won\'t work'
        )
      }
    },
    onReplace(location) {
      if (this.props.history) {
        this.props.history.replace(location)
      } else {
        warning(
          false,
          'Missing <Router history> prop; <Redirect>s won\'t work'
        )
      }
    }
  }

  static childContextTypes = {
    router: routerType.isRequired
  }

  getChildContext() {
    return {
      router: {
        createHref: this.createHref,
        transitionTo: this.transitionTo,
        replaceWith: this.replaceWith,
        blockTransitions: this.blockTransitions
      }
    }
  }

  createHref = (to) => {
    return this.props.createHref.call(this, to)
  }

  transitionTo = (location) => {
    this.props.onPush.call(this, location)
  }

  replaceWith = (location) => {
    this.props.onReplace.call(this, location)
  }

  blockTransitions = (getPromptMessage) => {
    return this.props.history.listenBefore(getPromptMessage)
  }

  render() {
    const { children, history, location } = this.props

    return (
      <History history={history} location={location}>
        {({ location }) => (
          <MatchCountProvider>
            {typeof children === 'function' ? (
              children({ location })
            ) : React.Children.count(children) > 1 ? (
              <div>{children}</div>
            ) : (
              children
            )}
          </MatchCountProvider>
        )}
      </History>
    )
  }
}

export default Router

@@ -1446,38 +1446,29 @@
 ef = (to) =%3E
- %7B
 %0A    
-return 
 this.props.c
@@ -1491,20 +1491,16 @@
 his, to)
-%0A  %7D
 %0A%0A  tran
@@ -1515,34 +1515,32 @@
  = (location) =%3E
- %7B
 %0A    this.props.
@@ -1562,28 +1562,24 @@
 s, location)
-%0A  %7D
 %0A%0A  replaceW
@@ -1593,26 +1593,24 @@
 location) =%3E
- %7B
 %0A    this.pr
@@ -1643,20 +1643,16 @@
 ocation)
-%0A  %7D
 %0A%0A  bloc
@@ -1687,30 +1687,21 @@
 sage) =%3E
- %7B
 %0A    
-return 
 this.pro
@@ -1741,20 +1741,16 @@
 Message)
-%0A  %7D
 %0A%0A  rend

Use expressions instead of function bodies