import MessagingActionTypes from "./messaging.types";

const INITIAL_STATE = {
  visible: false,
  selectedConversationId: null,
  isSending: false,
  error: null,
  message: null,
};

const messagingReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case MessagingActionTypes.SET_MESSAGE:
      return { ...state, message: action.payload };
    case MessagingActionTypes.TOGGLE_CHAT_VISIBLE:
      return {
        ...state,
        visible: !state.visible,
      };
    case MessagingActionTypes.SET_SELECTED_CONVERSATION:
      return {
        ...state,
        visible: true,
        selectedConversationId: action.payload,
      };
    case MessagingActionTypes.SEND_MESSAGE:
      return {
        ...state,
        error: null,
        isSending: true,
      };
    case MessagingActionTypes.SEND_MESSAGE_SUCCESS:
      return {
        ...state,
        message: "",
        isSending: false,
      };
    case MessagingActionTypes.SEND_MESSAGE_FAILURE:
      return {
        ...state,
        error: action.payload,
      };
    default:
      return state;
  }
};

export default messagingReducer;
