"use strict";

var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
_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 _events = require("../events");
var _activityMenu = require("./activity-menu");
function changeCurrentActivity(props) {
  var activityTypes = props.activityTypes,
    setCurrentActivity = props.setCurrentActivity;

  // Check if activity is valid
  var toActivity = activityTypes.find(function (activity) {
    return activity.name === setCurrentActivity;
  });
  if (toActivity) {
    if (toActivity.type === _activityMenu.ACTIVITY_TYPE_PRIMARY) {
      props.updateActivityType(setCurrentActivity);
    }
    if (toActivity.type === _activityMenu.ACTIVITY_TYPE_SECONDARY) {
      // Secondary activities need to have the menu opened
      props.updateSecondaryActivityType(setCurrentActivity);
    }
    // Emit Event
    props.onEvent(_events.eventNames.ACTIVITY_CHANGED, toActivity);
  }
}
function checkForExternalTrigger(nextProps, currentProps) {
  var setCurrentActivity = nextProps.setCurrentActivity;

  // Change Current Activity
  if (setCurrentActivity && setCurrentActivity !== currentProps.setCurrentActivity) {
    changeCurrentActivity(nextProps);
  }
}
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)({
  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    checkForExternalTrigger(nextProps, this.props);
  }
}));