import {createSelector} from 'reselect';
import {fromJS} from 'immutable';
import messages from './messages';

const getWidget = (state) => state.widgetSpace;
const getSpark = (state) => state.spark;
const getMedia = (state) => state.media;
const getConversation = (state) => state.conversation;
const getUsers = (state) => state.user;
const getAvatars = (state) => state.avatar;
const getFormatMessage = (state, props) => props.intl.formatMessage;

export const getSpaceDetails = createSelector(
  [getWidget, getConversation, getUsers, getAvatars],
  // eslint-disable-next-line max-params,complexity
  (widget, conversation, users, avatars) => {
    const spaceDetails = widget.get(`spaceDetails`);
    if (conversation.get(`id`)) {
      let title = spaceDetails.get(`title`);
      let avatar = conversation.get(`avatar`);
      const currentUserEmail = users.get(`currentUser`).email;

      // Grab avatar if we're in a direct conversation
      if (spaceDetails.get(`type`) === `direct`) {
        const toUser = conversation.get(`participants`)
          .filter((p) => p.get(`emailAddress`) !== currentUserEmail).first();
        title = toUser.get(`displayName`);
        avatar = avatars.getIn([`items`, toUser.get(`id`)]);
      }

      return widget.get(`spaceDetails`).merge({
        avatar,
        title
      });
    }
    return spaceDetails;
  }
);

export const getActivityTypes = createSelector(
  [getWidget],
  (widget) => {
    const spaceType = widget.getIn([`spaceDetails`, `type`]);
    const activityTypes = widget.get(`activityTypes`);
    // Filter activity Type based on spaceType
    if (spaceType) {
      return activityTypes.filter((a) => a && a.get(`spaceTypes`).includes(spaceType));
    }
    return activityTypes;
  }
);

const getWidgetError = createSelector(
  [getWidget, getSpark, getFormatMessage],
  (widget, spark, formatMessage) => {
    if (widget.getIn([`status`, `isError`]) || spark.getIn([`status`, `registerError`])) {
      const error = widget.get(`error`) || spark.get(`error`);
      const errorTitle = formatMessage(messages.unableToLoad);
      let errorSubtitle;
      switch (error.name) {
      case `Bad Space Id`:
        errorSubtitle = formatMessage(messages.badSpaceId);
        break;
      case `NotFound`:
        errorSubtitle = formatMessage(messages.errorNotFound);
        break;
      case `InvalidClientError`:
        errorSubtitle = formatMessage(messages.errorBadToken);
        break;
      default:
        errorSubtitle = formatMessage(messages.unknownError);
      }
      return fromJS({
        isError: true,
        errorTitle,
        errorSubtitle
      });
    }
    return fromJS({isError: false});
  });

export const getSpaceWidgetProps = createSelector(
  [getWidget, getSpark, getMedia, getSpaceDetails, getActivityTypes, getWidgetError],
  (widget, spark, media, spaceDetails, activityTypes, widgetError) => ({ // eslint-disable-line max-params
    activityTypes,
    media,
    sparkInstance: spark.get(`spark`),
    sparkState: spark.get(`status`),
    spaceDetails,
    widgetError,
    widgetStatus: widget.get(`status`)
  })
);


@@ -1872,25 +1872,59 @@
 essage) =%3E %7B
+ // eslint-disable-line complexity
 %0A
-
     if (widg
@@ -2509,17 +2509,521 @@
 default:
+ %7B
 %0A
+        // Attempt to inspect error.message due to minification or other errors%0A        if (error.message.contains(%60Could not find a room%60)) %7B%0A          errorSubtitle = formatMessage(messages.errorNotFound);%0A        %7D%0A        else if (error.message.contains(%60Bad Space Id%60)) %7B%0A          errorSubtitle = formatMessage(messages.badSpaceId);%0A        %7D%0A        else if (error.message.contains(%60Invalid client%60)) %7B%0A          errorSubtitle = formatMessage(messages.errorBadToken);%0A        %7D%0A        else %7B%0A  
         
@@ -3076,16 +3076,34 @@
 Error);%0A
+        %7D%0A      %7D%0A
       %7D%0A

check error message if error name not matched