import invariant from 'invariant'
import { PUSH, POP } from './Actions'
import { canUseDOM } from './ExecutionEnvironment'
import { addEventListener, removeEventListener, getWindowPath, supportsHistory } from './DOMUtils'
import { saveState, readState } from './DOMStateStorage'
import createDOMHistory from './createDOMHistory'

/**
 * Creates and returns a history object that uses HTML5's history API
 * (pushState, replaceState, and the popstate event) to manage history.
 * This is the recommended method of managing history in browsers because
 * it provides the cleanest URLs.
 *
 * Note: In browsers that do not support the HTML5 history API full
 * page reloads will be used to preserve URLs.
 */
function createBrowserHistory(options={}) {
  invariant(
    canUseDOM,
    'Browser history needs a DOM'
  )

  let { forceRefresh } = options

  let isSupported = supportsHistory() && !forceRefresh

  function getCurrentLocation(historyState) {
    historyState = historyState || window.history.state || {}

    let path = getWindowPath()
    let { key } = historyState

    let state
    if (key) {
      state = readState(key)
    } else {
      state = null
      key = history.createKey()

      if (isSupported)
        window.history.replaceState({ ...historyState, key }, null, path)
    }

    return history.createLocation(path, state, undefined, key)
  }

  function startPopStateListener({ transitionTo }) {
    function popStateListener(event) {
      if (event.state === undefined)
        return // Ignore extraneous popstate events in WebKit.

      transitionTo(
        getCurrentLocation(event.state)
      )
    }

    addEventListener(window, 'popstate', popStateListener)

    return function () {
      removeEventListener(window, 'popstate', popStateListener)
    }
  }

  function finishTransition(location) {
    let { basename, pathname, search, hash, state, action, key } = location

    if (action === POP)
      return // Nothing to do.

    saveState(key, state)

    let path = (basename || '') + pathname + search + hash
    let historyState = {
      key
    }

    if (action === PUSH) {
      if (isSupported) {
        window.history.pushState(historyState, null, path)
      } else {
        // Use a full-page reload to preserve the URL.
        window.location.href = path
      }
    } else { // REPLACE
      if (isSupported) {
        window.history.replaceState(historyState, null, path)
      } else {
        // Use a full-page reload to preserve the URL.
        window.location.replace(path)
      }
    }
  }

  let history = createDOMHistory({
    ...options,
    getCurrentLocation,
    finishTransition,
    saveState
  })

  let listenerCount = 0, stopPopStateListener

  function listenBefore(listener) {
    if (++listenerCount === 1)
      stopPopStateListener = startPopStateListener(history)

    let unlisten = history.listenBefore(listener)

    return function () {
      unlisten()

      if (--listenerCount === 0)
        stopPopStateListener()
    }
  }

  function listen(listener) {
    if (++listenerCount === 1)
      stopPopStateListener = startPopStateListener(history)

    let unlisten = history.listen(listener)

    return function () {
      unlisten()

      if (--listenerCount === 0)
        stopPopStateListener()
    }
  }

  // deprecated
  function registerTransitionHook(hook) {
    if (++listenerCount === 1)
      stopPopStateListener = startPopStateListener(history)

    history.registerTransitionHook(hook)
  }

  // deprecated
  function unregisterTransitionHook(hook) {
    history.unregisterTransitionHook(hook)

    if (--listenerCount === 0)
      stopPopStateListener()
  }

  return {
    ...history,
    listenBefore,
    listen,
    registerTransitionHook,
    unregisterTransitionHook
  }
}

export default createBrowserHistory

@@ -843,17 +843,16 @@
 options%0A
-%0A
   let is
@@ -884,13 +884,44 @@
 ry()
- && !
+%0A  let useRefresh = !isSupported %7C%7C 
 forc
@@ -2159,35 +2159,34 @@
 %7B%0A      if (
-isSupported
+useRefresh
 ) %7B%0A        
@@ -2196,51 +2196,28 @@
 dow.
-history.pushState(historyState, null,
+location.href =
  path
-)
 %0A   
@@ -2240,90 +2240,58 @@
     
-// Use a full-page reload to preserve the URL.%0A        window.location.href =
+window.history.pushState(historyState, null,
  path
+)
 %0A   
@@ -2329,27 +2329,26 @@
     if (
-isSupported
+useRefresh
 ) %7B%0A    
@@ -2362,157 +2362,102 @@
 dow.
-history.replaceState(historyState, null, path)%0A      %7D else %7B%0A        // Use a full-page reload to preserve the URL.%0A        window.location.replace(
+location.replace(path)%0A      %7D else %7B%0A        window.history.replaceState(historyState, null, 
 path

Rename var