text: import {createSelector} from 'reselect';
  
  import {destinationTypes} from './';
  
  const getWidget = (state) => state.widgetMeet;
  const getCalls = (state) => state.media.byId;
  const getAvatars = (state) => state.avatar;
  const getUsers = (state) => state.users;
  const getSpaces = (state) => state.spaces;
  const getOwnProps = (state, ownProps) => ownProps;
  
  const getCall = createSelector(
    [getOwnProps, getWidget, getCalls],
    (ownProps, widget, calls) => {
      let call;
      if (ownProps.call) {
        ({call} = ownProps);
      }
      else if (calls.has(widget.callId)) {
        call = calls.get(widget.callId);
      }
      else {
        ({call} = widget);
      }
  
      // Patching in activeParticipantsCount with devices to support sip should be migrated to sdk
      if (call && call.instance && call.instance.locus && call.instance.locus.participants) {
        const count = call.instance.locus.participants.reduce((acc, p) => acc + (p.state === 'JOINED' ? 1 : 0), 0);
        call = call.set('activeParticipantsCount', count);
      }
      return call;
    }
  );
  
  const getAvatarImage = createSelector(
    [getOwnProps, getAvatars, getWidget, getUsers],
    (ownProps, avatars, widget, users) => {
      const {
        toValue,
        toType
      } = widget;
  
      let avatarId;
      if (toType === destinationTypes.EMAIL) {
        avatarId = users.getIn(['byEmail', toValue]);
      }
      else if ([destinationTypes.EMAIL, destinationTypes.USERID, destinationTypes.SPACEID].includes(toType)) {
        avatarId = toValue;
      }
  
      return avatars.getIn(['items', avatarId]);
    }
  );
  
  const getDisplayName = createSelector(
    [getWidget, getUsers, getSpaces],
    (widget, users, spaces) => {
      const {
        toValue,
        toType
      } = widget;
  
      switch (toType) {
        case destinationTypes.SIP:
          return toValue;
        case destinationTypes.EMAIL: {
          const userId = users.getIn(['byEmail', toValue]);
          return users.getIn(['byId', userId, 'displayName']);
        }
        case destinationTypes.USERID:
          return users.getIn(['byId', toValue, 'displayName']);
        case destinationTypes.SPACEID:
          return spaces.getIn(['byId', toValue, 'displayName']);
        default:
          return '';
      }
    }
  );
  
  const getMeetWidgetProps = createSelector(
    [
      getAvatarImage,
      getWidget,
      getCall,
      getDisplayName
    ],
    (avatarImage, widgetMeet, call, displayName) => {
      const props = {
        avatarImage,
        displayName,
        call,
        widgetMeet,
        localVideoPosition: widgetMeet.localVideoPosition
      };
  
      let callProps;
  
      if (call) {
        // Is the call active?
        const {
          direction,
          isConnected,
          isDismissed,
          hasJoinedOnThisDevice,
          isRinging,
          isCall
        } = call;
  
        callProps = {
          isRinging,
          isCall,
          callInstance: call.instance,
          isActive: isConnected && hasJoinedOnThisDevice || direction === 'out',
          isIncoming: direction === 'in' && !hasJoinedOnThisDevice && !isDismissed
        };
      }
  
      return {
        ...props,
        ...callProps
      };
    }
  );
  
  export default getMeetWidgetProps;
  