import { fromJS } from 'immutable';

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

export const initialState = fromJS({
  loading: false,
  error: false,
  currentUser: false,
  auth: [],
  userData: {
    repositories: false,
  },
  userMode: '',
  loginName: '',
});

/* 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,
      });
    case SET_AUTHORITY:
      return state.merge({
        auth: action.auth,
      });
    case SET_USER_MODE:
      return state.merge({
        userMode: action.userMode,
      });
    case SET_LOGIN_NAME:
      return state.merge({
        loginName: action.loginName,
      });
    default:
      return state;
  }
};
export default appReducer;
