import * as ActionTypes from '../actionTypes'
import { RelaysDispatchTypes, RelaysState } from './types'

const initialState: RelaysState = {
  map: undefined,
  mapUpdated: undefined,
  info: undefined
}

const reducer = (
  state = initialState,
  action: RelaysDispatchTypes
): RelaysState | null => {
  switch (action.type) {
    case ActionTypes.SET_RELAY_MAP:
      return { ...state, map: action.payload, mapUpdated: Date.now() }

    case ActionTypes.SET_RELAY_MAP_UPDATED:
      return { ...state, mapUpdated: Date.now() }

    case ActionTypes.SET_RELAY_INFO:
      return {
        ...state,
        info: { ...state.info, ...action.payload }
      }

    case ActionTypes.RESTORE_STATE:
      return action.payload.relays

    default:
      return state
  }
}

export default reducer
