import UserActionTypes from "./user.types";

const INITIAL_STATE = {
  currentUser: {
    authorized: null,
  },
  bodyshop: null,
  signingIn: false,
  error: null,
  theme: "system"
};

const userReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case UserActionTypes.EMAIL_SIGN_IN_START:
      return {
        ...state,
        signingIn: true,
        error: null,
      };
    case UserActionTypes.SIGN_IN_SUCCESS:
      return {
        ...state,
        signingIn: false,
        currentUser: action.payload,
        error: null,
      };
    case UserActionTypes.SIGN_OUT_SUCCESS:
      return {
        ...state,
        currentUser: { authorized: false },
        error: null,
      };
    case UserActionTypes.UNAUTHORIZED_USER:
      return {
        ...state,
        error: null,
        currentUser: { authorized: false },
      };

    case UserActionTypes.UPDATE_USER_DETAILS_SUCCESS:
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          ...action.payload, //Spread current user details in.
        },
      };

    case UserActionTypes.SET_SHOP_DETAILS:
      return { ...state, bodyshop: action.payload };

    case UserActionTypes.SIGN_IN_FAILURE:
    case UserActionTypes.SIGN_OUT_FAILURE:
    case UserActionTypes.EMAIL_SIGN_UP_FAILURE:
      return {
        ...state,
        signingIn: false,
        error: action.payload,
      };
    default:
      return state;
  }
};

export default userReducer;
