import { ActionType, AuthActions, ChatActions } from '../actions';
import { Conversation } from '../models';

const INITIAL_STATE: Array<Conversation> = [];

export function conversationReducer(state = INITIAL_STATE, action: ActionType): Array<Conversation> {
  switch (action.type) {
    case ChatActions.ActionKey.CONVERSATION_UPDATED:
    case ChatActions.ActionKey.CONVERSATION_ADDDED:
      return [...state.filter((c) => c.id !== action.conversation.id), action.conversation];
    case AuthActions.ActionKey.LOGOUT_REQUESTED: {
      return INITIAL_STATE;
    }
    default:
      return state;
  }
}
