import * as ActionTypes from '../actionTypes'
import { ServersDispatchTypes, FileServersState } from './types'

const initialState: FileServersState = {
  map: undefined,
  mapUpdated: undefined
}

const reducer = (
  state = initialState,
  action: ServersDispatchTypes
): FileServersState => {
  switch (action.type) {
    case ActionTypes.SET_SERVER_MAP:
      return { ...state, map: action.payload, mapUpdated: Date.now() }

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

    case ActionTypes.RESTORE_STATE:
      return action.payload.servers || initialState

    default:
      return state
  }
}

export default reducer
