/* eslint-disable import/prefer-default-export */
import {
  constructMessagesEventData,
  constructRoomsEventData
} from './events';


/**
 * Processes a mercury event "event:conversation.activity"
 * @param {object} event
 * @param {object} eventNames
 * @param {string} currentUserId
 * @param {string} space
 * @param {object} actions
 * @param {function} actions.handleEvent
 * @param {function} actions.removeInflightActivity
 * @param {function} actions.updateHasNewMessage
 */
export function handleConversationActivityEvent(event, eventNames, currentUserId, space, actions) {
  const {activity} = event.data;
  const toUser = space.toPerson;
  const isSelf = activity.actor.id === currentUserId;

  // Ignore activity from other conversations
  if (activity.target && activity.target.id === space.id) {
    switch (activity.verb) {
      case 'acknowledge':
        if (activity.object.objectType === 'activity' && isSelf) {
          actions.handleEvent(eventNames.SPACES_READ, constructRoomsEventData(space, activity));
        }
        break;
      case 'share':
      case 'post':
        if (isSelf) {
          // Remove the in flight activity that matches this
          actions.removeInflightActivity(activity.clientTempId);
        }
        else {
          actions.updateHasNewMessage(true);
          actions.handleEvent(eventNames.SPACES_UNREAD, constructRoomsEventData(space, activity));
        }
        // Emit message:created event
        actions.handleEvent(eventNames.MESSAGES_CREATED, constructMessagesEventData(activity, toUser));
        break;
      default: {
        break;
      }
    }
  }
}

/* eslint-disable import/prefer-default-export */
import {
  constructMessagesEventData,
  constructRoomsEventData
} from './events';


/**
 * Processes a mercury event "event:conversation.activity"
 * @param {object} event
 * @param {object} eventNames
 * @param {string} currentUserId
 * @param {string} space
 * @param {object} actions
 * @param {function} actions.handleEvent
 * @param {function} actions.removeInflightActivity
 * @param {function} actions.updateHasNewMessage
 */
export function handleConversationActivityEvent(event, eventNames, currentUserId, space, actions) {
  const {activity} = event.data;
  const toUser = space.toUser && space.toUser.toJS();
  const isSelf = activity.actor.id === currentUserId;

  // Ignore activity from other conversations
  if (activity.target && activity.target.id === space.id) {
    switch (activity.verb) {
      case 'acknowledge':
        if (activity.object.objectType === 'activity' && isSelf) {
          actions.handleEvent(eventNames.SPACES_READ, constructRoomsEventData(space, activity));
        }
        break;
      case 'share':
      case 'post':
        if (isSelf) {
          // Remove the in flight activity that matches this
          actions.removeInflightActivity(activity.clientTempId);
        }
        else {
          actions.updateHasNewMessage(true);
          actions.handleEvent(eventNames.SPACES_UNREAD, constructRoomsEventData(space, activity));
        }
        // Emit message:created event
        actions.handleEvent(eventNames.MESSAGES_CREATED, constructMessagesEventData(activity, toUser));
        break;
      default: {
        break;
      }
    }
  }
}

use proper toUser object in activity event