<commit_before>import {compose, lifecycle} from 'recompose';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import {fetchAvatar} from '@ciscospark/redux-module-avatar';
import {resetErrors} from '@ciscospark/redux-module-errors';
import {connectToMercury} from '@ciscospark/redux-module-mercury';
import {fetchSpace} from '@ciscospark/redux-module-spaces';
import {getUser} from '@ciscospark/redux-module-users';
import {validateAndDecodeId} from '@ciscospark/react-component-utils';

import {
  getSpaceDetails,
  reloadWidget,
  storeDestination,
  updateWidgetStatus
} from '../actions';

import {getSpaceWidgetProps} from '../selector';

import {destinationTypes} from '../';

function setup(props, prevProps) {
  const {
    conversation,
    destination,
    errors,
    sparkInstance,
    sparkState,
    spaceDetails,
    widgetStatus,
    mercuryStatus,
    users,
    spaces
  } = props;

  if (sparkInstance
    && sparkState.get('authenticated')
    && sparkState.get('registered')
    && !sparkState.get('hasError')
  ) {
    // Check if we need to reload space
    if (widgetStatus.shouldReloadWidget && prevProps && !prevProps.widgetStatus.shouldReloadWidget) {
      props.reloadWidget();
      props.resetErrors();
    }

    // Check for destination Change
    if (destination && prevProps.destination) {
      const previousDestination = prevProps.destination;

      if (
        destination.id !== previousDestination.id
        || destination.type !== previousDestination.type
      ) {
        sparkInstance.logger.info('Destination has changed, widget reloading...');
        props.updateWidgetStatus({
          shouldReloadWidget: true
        });
      }
    }

    // Connect to websocket (mercury)
    if (!mercuryStatus.hasConnected
      && !mercuryStatus.connecting
      && !mercuryStatus.connected
      && sparkInstance.internal.device.registered) {
      props.connectToMercury(sparkInstance);
    }

    // Get space details for given destination
    if (!widgetStatus.isFetchingSpaceDetails && !errors.get('hasError') && !spaceDetails) {
      // If the selector isn't returning a destination object and we have props for them, store
      if (destination) {
        // Use destination object from store to fetch space details
        props.getSpaceDetails({
          sparkInstance,
          destinationId: destination.id,
          destinationType: destination.type,
          intl: props.intl
        });
        // Fetch 1:1 User details
        if (destination.type === destinationTypes.EMAIL && !users.getIn(['byEmail', destination.id])) {
          props.getUser({email: destination.id}, sparkInstance);
        }
        if (destination.type === destinationTypes.USERID && !users.getIn(['byId', destination.id])) {
          props.getUser({id: destination.id}, sparkInstance);
        }
        if (destination.type === destinationTypes.SPACEID && !spaces.getIn(['byId', validateAndDecodeId(destination.id)])) {
          props.fetchSpace(sparkInstance, validateAndDecodeId(destination.id));
        }
      }
      else {
        // Destination has not been stored yet, process and store.<commit_msg>refactor(widget-space): use standard getters in setup<commit_after>import {compose, lifecycle} from 'recompose';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

import {fetchAvatar} from '@ciscospark/redux-module-avatar';
import {resetErrors} from '@ciscospark/redux-module-errors';
import {connectToMercury} from '@ciscospark/redux-module-mercury';
import {fetchSpace} from '@ciscospark/redux-module-spaces';
import {getUser} from '@ciscospark/redux-module-users';
import {validateAndDecodeId} from '@ciscospark/react-component-utils';

import {
  getSpaceDetails,
  reloadWidget,
  storeDestination,
  updateWidgetStatus
} from '../actions';

import {getSpaceWidgetProps} from '../selector';

import {destinationTypes} from '../constants';

export function setup(props, prevProps = {}) {
  const {
    conversation,
    destination,
    errors,
    sparkInstance,
    sparkState,
    spaceDetails,
    widgetStatus,
    mercuryStatus,
    users,
    spaces
  } = props;

  if (sparkInstance
      && sparkState.authenticated
      && sparkState.registered
      && !sparkState.hasError
  ) {
    // Check if we need to reload space
    if (widgetStatus.shouldReloadWidget && prevProps && !prevProps.widgetStatus.shouldReloadWidget) {
      props.reloadWidget();
      props.resetErrors();
    }

    // Check for destination Change
    if (destination && prevProps.destination) {
      const previousDestination = prevProps.destination;

      if (
        destination.id !== previousDestination.id
        || destination.type !== previousDestination.type
      ) {
        sparkInstance.logger.info('Destination has changed, widget reloading...');
        props.updateWidgetStatus({
          shouldReloadWidget: true
        });
      }
    }

    // Connect to websocket (mercury)
    if (!mercuryStatus.hasConnected
      && !mercuryStatus.connecting
      && !mercuryStatus.connected
      && sparkState.registered) {
      props.connectToMercury(sparkInstance);
    }

    // Get space details for given destination
    if (!widgetStatus.isFetchingSpaceDetails && !errors.get('hasError') && !spaceDetails) {
      // If the selector isn't returning a destination object and we have props for them, store
      if (destination) {
        // Use destination object from store to fetch space details
        props.getSpaceDetails({
          sparkInstance,
          destinationId: destination.id,
          destinationType: destination.type,
          intl: props.intl
        });
        // Fetch 1:1 User details
        if (destination.type === destinationTypes.EMAIL && !users.getIn(['byEmail', destination.id])) {
          props.getUser({email: destination.id}, sparkInstance);
        }
        if (destination.type === destinationTypes.USERID && !users.getIn(['byId', destination.id])) {
          props.getUser({id: destination.id}, sparkInstance);
        }
        if (destination.type === destinationTypes.SPACEID && !spaces.getIn(['byId', validateAndDecodeId(destination.id)])) {
          props.fetchSpace(sparkInstance, validateAndDecodeId(destination.id));
        }
      }
      else {
        // Destination has not been stored yet, process and store.
{'fraction_non_alphanumeric': 0.11785881984661137, 'fraction_numerical': 0.0006260760682422914, 'mean_word_length': 3.127906976744186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1116842', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1761, 'n_words': 402}