import {
  GET_TODOS,
  ADD_TODO,
  UPDATE_TODO,
  DELETE_TODO,
  TOGGLE_COMPLETE_TODO,
  DELETE_COMPLETED_TODOS,
} from '../actions/todo-actions'

function initialState() {
  return {
    dataSource: {},
    data: [],
  }
}

export default function todos(state = initialState(), action = {}) {
  switch (action.type) {
    case GET_TODOS:
      return {
        dataSource: action.dataSource,
        data: Object.values(action.dataSource),
      }

    case ADD_TODO:
    case UPDATE_TODO:
    case DELETE_TODO:
    case TOGGLE_COMPLETE_TODO:
    case DELETE_COMPLETED_TODOS:
      return {
        ...state,
        data: Object.values(state.dataSource),
      }

    default:
      return state
  }
}
