import { fromJS } from 'immutable';

import {
  LOAD_REPOS,
  LOAD_REPOS_ERROR,
  LOAD_REPOS_SUCCESS,
} from './constants';

export const initialState = fromJS({
  loading: false,
  error: false,
  currentUser: false,
  userData: {
    repositories: false,
  },
});

/* eslint-disable default-case, no-param-reassign */
const appReducer = (state = initialState, action) => {
  switch (action.type) {
    case LOAD_REPOS:
      return state.merge({
        loading: true,
        error: false,
        userData: {
          repositories: false
        }
      })
    case LOAD_REPOS_SUCCESS:
      return state.merge({
        loading: false,
        currentUser: action.username,
        userData: {
          repositories: action.repos
        }
      })

    case LOAD_REPOS_ERROR:
      return state.merge({
        error: action.error,
        loading: false
      })
    default:
      return state;
  }
}
export default appReducer;
