/**
 * socket reducer
 */
import { applicationState } from "../../state"
import { clone } from "ramda"
import reducerFromMap from "@racletteCore/lib/reducerFromMap"

/**
 * set socket status
 */
const statusSocket = (state, action) => {
  const newState = clone(state)

  newState.socket = { ...newState.socket, ...action.payload }
  if (newState.socket.status === 1) {
    newState.socket.health = 1
  }

  return newState
}

const statusSearch = (state, action) => {
  const newState = clone(state)

  newState.search = { ...newState.search, ...action.payload }

  return newState
}
// map instead of switch statement
const reducerMap = {
  init: () => applicationState.backend,
  "backend/socket/status": statusSocket,
  "backend/search/status": statusSearch,
}

export default reducerFromMap(reducerMap)
