/* eslint-disable max-params */
import {createSelector} from 'reselect';

const getActivities = (state) => state.conversation.get(`activities`);
const getParticipants = (state) => state.conversation.get(`participants`);
const getCurrentUser = (state) => state.user.get(`currentUser`);
// Yes, avatar, not 'avatars'
const getAvatars = (state) => state.avatar;
const getIndicators = (state) => state.indicators;


export const getReadReceipts = createSelector(
  [getCurrentUser, getActivities, getParticipants, getAvatars, getIndicators],
  (currentUser, activities, participants, avatars, indicators) => {
    const activity = activities.last();
    const {typing} = indicators;

    const readParticipants = participants
    .filter((participant) =>
      participant.get(`id`) !== currentUser.id &&
      participant.getIn([`roomProperties`, `lastSeenActivityUUID`]) === activity.id);

    const limitParticipants = readParticipants.slice(0, 10)
    .map((participant) => {
      const participantId = participant.get(`id`);
      const image = avatars.getIn([`items`, participantId]);
      // Typing events don't give us user IDs, only emails.
      const isTyping = typing.includes(participant.get(`emailAddress`));
      return {
        displayName: participant.get(`displayName`),
        image,
        isTyping,
        userId: participantId
      };
    }).toJS();

    return {
      users: limitParticipants,
      totalCount: readParticipants.size
    };
  }
);

/* eslint-disable max-params */
import {createSelector} from 'reselect';

const getActivities = (state) => state.conversation.get(`activities`);
const getParticipants = (state) => state.conversation.get(`participants`);
const getCurrentUser = (state) => state.user.get(`currentUser`);
// Yes, avatar, not 'avatars'
const getAvatars = (state) => state.avatar;
const getTypingIndicators = (state) => state.indicators.get(`typing`);


export const getReadReceipts = createSelector(
  [getCurrentUser, getActivities, getParticipants, getAvatars, getTypingIndicators],
  (currentUser, activities, participants, avatars, typing) => {
    const activity = activities.last();
    const readParticipants = participants
    .filter((participant) =>
      participant.get(`id`) !== currentUser.id &&
      participant.getIn([`roomProperties`, `lastSeenActivityUUID`]) === activity.id);

    const limitParticipants = readParticipants.slice(0, 10)
    .map((participant) => {
      const participantId = participant.get(`id`);
      const image = avatars.getIn([`items`, participantId]);
      // Typing events don't give us user IDs, only emails.
      const isTyping = typing.has(participant.get(`emailAddress`));
      return {
        displayName: participant.get(`displayName`),
        image,
        isTyping,
        userId: participantId
      };
    }).toJS();

    return {
      users: limitParticipants,
      totalCount: readParticipants.size
    };
  }
);

Update to immutable