import { combineReducers } from "redux";

const initialTodosState = {
    entries: null,
    isLoading: false,
    error: null,
  };

function todos(state = initialTodosState, action) {
    switch (action.type) {
      case "FETCH_TODOS_STARTED":
        return { ...state, isLoading: true, error: null };
      case "FETCH_TODOS_SUCCEEDED":
        return { ...state, isLoading: false, entries: action.todos };
      case "FETCH_TODOS_FAILED":
        return { ...state, isLoading: false, error: action.error };
      default:
        return state;
    }
  }

  const initialEntriesState = {
    entries: null,
    isLoading: false,
    error: null,
  };
  function entries(state = initialEntriesState, action) {
    switch (action.type) {
      case "FETCH_ENTRIES_STARTED":
        return { ...state, isLoading: true, error: null };
      case "FETCH_ENTRIES_SUCCEEDED":
        return { ...state, isLoading: false, entries: action.entries };
      case "FETCH_ENTRIES_FAILED":
        return { ...state, isLoading: false, error: action.error };
      default:
        return state;
    }
  }

  const initialAuthState = {
    token: null,
    isAuthenticated: false,
    isLoading: false,
    error: null,
  };
  function auth(state = initialAuthState, action) {
    switch (action.type) {
      case "LOGIN_STARTED":
        return { ...state, isAuthenticated:false, isLoading: true, error: null };
      case "LOGIN_SUCCEEDED":
        return { ...state, isAuthenticated:true, isLoading: false, token: action.token };
      case "LOGIN_FAILED":
        return { ...state, isAuthenticated:false, isLoading: false, error: action.error };
      default:
        return state;
    }
  }
  
  const reducers = combineReducers({
    todos,
    entries,
    auth
  });
  
  export default reducers;