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

/**
 * set user
 */
const userSet = (state, action) => {
  if (state?._id) {
    return state
  }
  if (action?.payload?._id) {
    return action.payload
  }

  return state
}

/**
 * update user
 */
const userUpdate = (state, action) => {
  const userState = clone(state)

  return { ...userState, ...action.payload }
}

/**
 * define map
 */
const reducerMap = {
  init: () => applicationState.user,
  "user/set": userSet,
  "user/update": userUpdate,
}

export default reducerFromMap(reducerMap)
