"use strict";

var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactRedux = require("react-redux");
var _recompose = require("recompose");
var _redux = require("redux");
var _reduxModuleErrors = require("@webex/redux-module-errors");
var _reactComponentUtils = require("@webex/react-component-utils");
var _messages = _interopRequireDefault(require("../messages"));
var _constants = require("../constants");
var _activityMenu = require("./activity-menu");
function checkForMercuryErrors(props) {
  var errors = props.errors,
    intl = props.intl,
    mercury = props.mercury;
  // Add Mercury disconnect error
  var mercuryError = 'mercury.disconnect';
  var isMercuryConnected = mercury.getIn(['status', 'connected']);
  var isMercuryDisconnected = !isMercuryConnected && mercury.getIn(['status', 'hasConnected']);
  var hasError = errors.get('hasError');
  var hasMercuryError = errors.get('errors').has(mercuryError);
  var hasNoMercuryError = !hasError || !hasMercuryError;
  if (isMercuryDisconnected && hasNoMercuryError) {
    // Create UI Error
    var formatMessage = intl.formatMessage;
    props.addError({
      id: mercuryError,
      displayTitle: formatMessage(_messages.default.errorConnection),
      displaySubtitle: formatMessage(_messages.default.reconnecting),
      temporary: true
    });
  }
  if (isMercuryConnected && hasMercuryError) {
    props.removeError(mercuryError);
  }
}

/**
 * Checks for SDK device registration errors
 * @param {Object} props
 */
function checkForRegistrationErrors(props) {
  var errors = props.errors,
    sparkState = props.sparkState,
    spark = props.spark;
  var formatMessage = props.intl.formatMessage;
  var registerErrorId = 'spark.register';
  if (sparkState.get('registerError') && (!errors.get('hasError') || !errors.get('errors').has(registerErrorId))) {
    var error = spark.get('error');
    var displaySubtitle = formatMessage(_messages.default.unknownError);
    if (error.statusCode === 401) {
      displaySubtitle = formatMessage(_messages.default.errorBadToken);
    }
    props.addError({
      id: registerErrorId,
      displayTitle: formatMessage(_messages.default.unableToLoad),
      displaySubtitle: displaySubtitle,
      temporary: false,
      code: error.statusCode
    });
  }
}

/**
 * Verifies the destination provided is valid
 * @param {Object} props
 */
function checkForDestinationErrors(props) {
  var currentUser = props.currentUser,
    destination = props.destination,
    destinationId = props.destinationId,
    destinationType = props.destinationType,
    errors = props.errors;
  var formatMessage = props.intl.formatMessage;

  // Destination type and id are required
  var missingDestinationErrorId = 'space.error.missingDestination';
  if (!destinationId && !destinationType && (!errors.get('hasError') || !errors.get('errors').has(missingDestinationErrorId))) {
    // No destination found
    props.addError({
      id: missingDestinationErrorId,
      displayTitle: formatMessage(_messages.default.unableToLoad),
      displaySubtitle: formatMessage(_messages.default.unknownDestination),
      temporary: false
    });
  }

  // Cannot start a space with yourself
  var toSelfErrorId = 'space.error.toSelf';
  if (destination && currentUser && currentUser.id && currentUser.email && (!errors.get('hasError') || !errors.get('errors').has(toSelfErrorId))) {
    // Check for to user being self
    if (destination.type === _constants.destinationTypes.EMAIL && currentUser.email === destination.id || destination.type === _constants.destinationTypes.USERID && (0, _reactComponentUtils.validateAndDecodeId)(destination.id).id === currentUser.id) {
      props.addError({
        id: toSelfErrorId,
        displayTitle: formatMessage(_messages.default.unableToLoad),
        displaySubtitle: formatMessage(_messages.default.errorToSelf),
        temporary: false
      });
    }
  }

  // Cannot start a space to a UUID destination ID
  var invalidDestinationErrorId = 'space.error.invalidDestination';
  if (destinationId && (0, _reactComponentUtils.isUuid)(destinationId) && (!errors.get('hasError') || !errors.get('errors').has(invalidDestinationErrorId))) {
    // No destination found
    props.addError({
      id: invalidDestinationErrorId,
      displayTitle: formatMessage(_messages.default.unableToLoad),
      displaySubtitle: formatMessage(_messages.default.invalidDestination),
      temporary: false
    });
  }
}

/**
 * Verifies the prop based activity types are valid
 * @param {Object} props
 */
function checkActivityTypes(props) {
  var activityTypes = props.activityTypes,
    errors = props.errors,
    initialActivity = props.initialActivity;
  var formatMessage = props.intl.formatMessage;
  if (activityTypes && activityTypes.length > 0) {
    var invalidActivityId = 'ciscospark.container.space.error.invalidActivity';
    var defaultActivity = 'message';
    var validActivity = false;
    if (activityTypes.some(function (activity) {
      return activity.name === initialActivity;
    }) || activityTypes.some(function (activity) {
      return activity.name === defaultActivity;
    }) || activityTypes[0] && activityTypes[0].name && activityTypes[0].type === _activityMenu.ACTIVITY_TYPE_PRIMARY) {
      validActivity = true;
    }
    if (!validActivity && !errors.get('errors').has(invalidActivityId)) {
      props.addError({
        id: invalidActivityId,
        displayTitle: formatMessage(_messages.default.disabledInitialActivity),
        temporary: false
      });
    }
    if (validActivity && errors.get('errors').has(invalidActivityId)) {
      props.removeError(invalidActivityId);
    }
  }
}

/**
 * Verifies the prop based messaging options are valid
 * @param {Object} props
 */
function checkCanSendMessage(props) {
  var activityTypes = props.activityTypes,
    errors = props.errors,
    showSubmitButton = props.showSubmitButton,
    sendMessageOnReturnKey = props.sendMessageOnReturnKey;
  var formatMessage = props.intl.formatMessage;
  var defaultActivity = 'message';
  if (activityTypes && activityTypes.length > 0) {
    var invalidMessagingId = 'ciscospark.container.space.error.invalidSendMessageConfiguration';
    var hasEnabledMessaging = activityTypes.some(function (activity) {
      return activity.name === defaultActivity;
    });
    var hasErrorCurrently = errors.get('errors').has(invalidMessagingId);
    if (hasEnabledMessaging && !showSubmitButton && !sendMessageOnReturnKey && !hasErrorCurrently) {
      props.addError({
        id: invalidMessagingId,
        displayTitle: formatMessage(_messages.default.invalidSendMessageConfiguration),
        temporary: false
      });
    }
    if (hasEnabledMessaging && (showSubmitButton || sendMessageOnReturnKey) && hasErrorCurrently) {
      props.removeError(invalidMessagingId);
    }
  }
}
function checkForErrors(props) {
  checkForMercuryErrors(props);
  checkForRegistrationErrors(props);
  checkForDestinationErrors(props);
  checkActivityTypes(props);
  checkCanSendMessage(props);
}
var _default = exports.default = (0, _recompose.compose)((0, _reactRedux.connect)(function (state) {
  return state;
}, function (dispatch) {
  return (0, _redux.bindActionCreators)({
    addError: _reduxModuleErrors.addError,
    removeError: _reduxModuleErrors.removeError
  }, dispatch);
}), (0, _recompose.lifecycle)({
  componentWillMount: function componentWillMount() {
    checkForErrors(this.props);
  },
  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    checkForErrors(nextProps);
  }
}));