import PhotosActionTypes from "./photos.types";

const INITIAL_STATE = {
  photos: [],
  uploadInProgress: false,
  uploadError: null,
};

const photosReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case PhotosActionTypes.ADD_PHOTO:
      return {
        ...state,
        photos: [...state.photos, action.payload],
      };
    case PhotosActionTypes.REMOVE_ALL_PHOTOS:
      return {
        ...state,
        uploadInProgress: false,
        photos: [],
      };
    case PhotosActionTypes.REMOVE_PHOTOS:
      return {
        ...state,
        photos: state.photos.filter((p) => !action.payload.includes(p.id)),
      };
    case PhotosActionTypes.UPLOAD_PHOTO_FAILURE:
      return {
        ...state,
        uploadInProgress: false,
        uploadError: action.payload,
      };
    case PhotosActionTypes.UPLOAD_PHOTO_SUCCESS:
      return {
        ...state,
        uploadInProgress: false,
      };
    case PhotosActionTypes.UPLOAD_ALL_PHOTOS_START:
    case PhotosActionTypes.UPLOAD_SELECTED_PHOTOS_START:
      return {
        ...state,
        uploadInProgress: true,
        uploadError: null,
      };
    default:
      return state;
  }
};

export default photosReducer;
