import {
  combineReducers
} from 'redux'
import {
  HOTELS,
  POPULAR_HOTELS,
  HOTELS_DETAILS
} from '../../actions/actionTypes'
import {
  fetchingData,
  initStateLang
} from '../../utils'

const initailState = {
  loading: false,
  error: '',
  data: [],
  lastUpdate: ''
}

const list = (state = initStateLang(initailState, ['data', 'lastUpdate']), action) => {
  switch (action.type) {
    case HOTELS.REQUEST:
    return {
      ...state,
      loading: true,
      error: '',
    }
  case HOTELS.SUCCESS:
    return {
      ...state,
      loading: false,
      error: '',
      [action.lang]: { data: action.data, lastUpdate: action.lasted }
    }
  case HOTELS.FAILURE:
    return {
      ...state,
      loading: false,
      error: action.error,
    }
  default:
    return state
  }
}
const popular = (state = initStateLang(initailState, ['data', 'lastUpdate']), action) => {
  switch (action.type) {
    case POPULAR_HOTELS.REQUEST:
      return {
        ...state,
        loading: true,
        error: '',
      }
    case POPULAR_HOTELS.SUCCESS:
      return {
        ...state,
        loading: false,
        error: '',
        [action.lang]: { data: action.data, lastUpdate: action.lasted }
      }
    case POPULAR_HOTELS.FAILURE:
      return {
        ...state,
        loading: false,
        error: action.error,
      }
    default:
      return state
  }
}
const initailStateDetail = {
  loading: false,
  error: '',
  data: {},
  lastUpdate: ''
}
const details = (state = initStateLang(initailStateDetail, ['data', 'lastUpdate']), action) => {
  switch (action.type) {
    case HOTELS_DETAILS.REQUEST:
      return {
        ...state,
        loading: true,
        error: '',
        [action.lang]: { data: {} }
      }
    case HOTELS_DETAILS.SUCCESS:
      return {
        ...state,
        loading: false,
        error: '',
        [action.lang]: { data: action.data, lastUpdate: action.lasted }
      }
    case HOTELS_DETAILS.FAILURE:
      return {
        ...state,
        loading: false,
        error: action.error,
      }
    default:
      return state
  }
}
export default combineReducers({
  list,
  popular,
  details
})