import { replace } from 'react-router-redux'

export function toggleParameter (queryParamName, inputLocationQuery) {
  return (dispatch, getState) => {
    const pathname = getState().routing.locationBeforeTransitions.pathname
    const locationQuery = inputLocationQuery || { ...getState().routing.locationBeforeTransitions.query }
    const queryParam = locationQuery[ queryParamName ]
    if (queryParam !== undefined) {
      delete locationQuery[ queryParamName ]
    } else {
      locationQuery[ queryParamName ] = null // null enables the parameter
    }
    return dispatch(replace({ pathname: pathname, query: locationQuery }))
  }
}

export function toggleParameterValue (queryParamName, value, inputLocationQuery, shouldRemoveInBackString = false, nameToReplace) {
  return (dispatch, getState) => {
    const pathname = getState().routing.locationBeforeTransitions.pathname
    const locationQuery = inputLocationQuery || { ...getState().routing.locationBeforeTransitions.query }
    let queryParam = locationQuery[ queryParamName ] || []
    if (shouldRemoveInBackString) {
      locationQuery[ queryParamName ] = queryParam.replace(`${nameToReplace}=${value}&`, '')
    } else {
      if (!Array.isArray(queryParam)) {
        queryParam = [ queryParam ]
      }
      if (queryParam.includes(value)) {
        queryParam = queryParam.filter(queryParamValue => queryParamValue !== value)
      } else {
        queryParam.push(value)
      }
      if (queryParam.length === 0) {
        delete locationQuery[ queryParamName ]
      } else {
        locationQuery[ queryParamName ] = queryParam
      }
    }
    return dispatch(replace({ pathname: pathname, query: locationQuery }))
  }
}

@@ -2,23 +2,20 @@
 mport %7B 
-replace
+push
  %7D from 
@@ -565,39 +565,36 @@
 return dispatch(
-replace
+push
 (%7B pathname: pat
@@ -1630,23 +1630,20 @@
 ispatch(
-replace
+push
 (%7B pathn

fix back/forward navigation after filter clicks