import defaults from 'lodash/defaults'
import pick from 'lodash/pick'
import { newKey } from './utils'

export const HISTORY_CREATE = '@@history/CREATE'
export const HISTORY_RESTORE = '@@history/RESTORE'
export const HISTORY_HASH_CHANGE = '@@history/HISTORY_HASH_CHANGE'

/**
 * This action should be dispatched when you want to exchange state with a previous history.
 */
export function restore(key, pushState = true) {
  return {
    type: HISTORY_RESTORE,
    payload: key,
    meta: {
      pushState,
    },
  }
}
const defaultLocation = {
  pathname: '',
  hash: '',
  search: '',
}
export function getLocationObject(location) {
  const loc = pick(location,
    'pathname', 'hash', 'search', 'origin', 'protocol', 'port', 'hostname'
  )
  return defaults(loc, defaultLocation)
}
/**
 * This action should be dispatched when you want a new history entry.
 */
export function create(location, title, key = null, pushState = true) {
  return {
    type: HISTORY_CREATE,
    payload: {
      title: title || '',
      location: getLocationObject(location),
      key: key || newKey(),
    },
    meta: {
      pushState,
    },
  }
}

// Currently this is private only. Call when user changes hash in address bar.
export function hashChange(hash) {
  return {
    type: HISTORY_HASH_CHANGE,
    payload: {
      hash,
      key: newKey(),
    },
  }
}

@@ -1132,17 +1132,21 @@
   %7D%0A%7D%0A%0A/
-/
+**%0A *
  Current
@@ -1167,19 +1167,16 @@
 vate
- only
 . Call
+ed
  whe
@@ -1211,16 +1211,20 @@
 ss bar.%0A
+ */%0A
 export f

format hashChange comment like others