"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.propTypes = exports.default = exports.ACTIVITY_TYPE_SECONDARY = exports.ACTIVITY_TYPE_PRIMARY = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/typeof"));
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactRedux = require("react-redux");
var _redux = require("redux");
var _recompose = require("recompose");
var _classnames = _interopRequireDefault(require("classnames"));
var _widgetMessage = _interopRequireDefault(require("@webex/widget-message"));
var _widgetMeet = _interopRequireDefault(require("@webex/widget-meet"));
var _widgetRoster = _interopRequireDefault(require("@webex/widget-roster"));
var _widgetFiles = _interopRequireDefault(require("@webex/widget-files"));
var _react2 = require("@momentum-ui/react");
var _reduxModuleFeatures = require("@webex/redux-module-features");
var _actions = require("../actions");
var _events = require("../events");
var styles = {
  "spaceWidget": "spaceWidget--3yj91",
  "activityComponentWrapper": "activityComponentWrapper--18hc7",
  "widgetBody": "widgetBody--3Ljcg",
  "hidden": "hidden--2_dL1",
  "messageButton": "messageButton--2XtoE",
  "meetButton": "meetButton--2x_-c",
  "peopleButton": "peopleButton--3Iori",
  "filesButton": "filesButton--_jeYc",
  "activityMenuButtonWrapper": "activityMenuButtonWrapper--jVljv",
  "tabContainer": "tabContainer--1dcOa",
  "tabMeet": "tabMeet--1J4Z4",
  "huddle": "huddle--1clnu",
  "tabOther": "tabOther--3ZSMv",
  "tabActive": "tabActive--tcqll",
  "activityMenuButton": "activityMenuButton--1U7tK",
  "activityMenuWrapper": "activityMenuWrapper--1mCc5",
  "slideIn": "slideIn--1B82D",
  "callTimer": "callTimer--2ejN5",
  "errorWrapper": "errorWrapper--3b8qq",
  "secondaryWidget": "secondaryWidget--3x3YW",
  "secondaryWidgetCover": "secondaryWidgetCover--Rd4Fj",
  "secondaryWidgetFull": "secondaryWidgetFull--2qTh-"
}; // @related-file ../styles.css
var ACTIVITY_TYPE_PRIMARY = exports.ACTIVITY_TYPE_PRIMARY = 'ACTIVITY_TYPE_PRIMARY';
var ACTIVITY_TYPE_SECONDARY = exports.ACTIVITY_TYPE_SECONDARY = 'ACTIVITY_TYPE_SECONDARY';
var DEFAULT_ACTIVITY = 'message';
var propTypes = exports.propTypes = {
  initialActivity: _propTypes.default.string
};

/**
 * Builds widget elements from activityTypes list
 *
 * @param {List} activityTypes Immutable List of activity types
 * @param {String} primaryActivityType Active activity type
 * @returns {Array} of Widget elements
 */

function constructActivityWidgets(props) {
  return function (activityTypes, primaryActivityType, composerActions) {
    return activityTypes.filter(function (activityType) {
      return activityType.type !== ACTIVITY_TYPE_SECONDARY;
    }).map(function (a) {
      // Only allow widget if it's available for this space type
      var Widget = a.component;
      var isVisible = primaryActivityType === a.name ? '' : styles.hidden;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("webex-".concat(a.name, "-wrapper"), styles.activityComponentWrapper, isVisible),
        key: a.name
      }, /*#__PURE__*/_react.default.createElement(Widget, (0, _extends2.default)({}, props, {
        composerActions: composerActions,
        eventNames: _events.eventNames,
        injectProvider: false,
        toPerson: props.spaceDetails.toPerson
      })));
    });
  };
}

/**
 * Builds secondary widget elements
 *
 * @param {Object} props
 * @returns {Function}
 */
function constructSecondaryActivityWidget(props) {
  return function (activityTypes, secondaryActivityType) {
    if (!secondaryActivityType) {
      return null;
    }
    var secondaryWidgetClassNames = ['webex-secondary-widget', styles.secondaryWidget,
    // eslint-disable-next-line react/prop-types
    props.secondaryActivitiesFullWidth ? styles.secondaryWidgetFull : styles.secondaryWidgetCover];
    return activityTypes.filter(function (activityType) {
      return activityType.type === ACTIVITY_TYPE_SECONDARY && activityType.name === secondaryActivityType;
    }).map(function (activityType) {
      var Widget = activityType.component;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)(secondaryWidgetClassNames),
        key: activityType.name
      }, /*#__PURE__*/_react.default.createElement(Widget, (0, _extends2.default)({}, props, activityType.props, {
        eventNames: _events.eventNames,
        injectProvider: false
      })));
    });
  };
}
function handleActivityChange(props) {
  return function (activity) {
    if (activity.type === ACTIVITY_TYPE_PRIMARY) {
      props.updateActivityType(activity.name);
    } else {
      props.updateSecondaryActivityType(activity.name);
    }
    // Emit Event
    props.onEvent(_events.eventNames.ACTIVITY_CHANGED, activity);
  };
}
function handleSecondaryDismiss(props) {
  return function () {
    return props.updateSecondaryActivityType(null);
  };
}
function handleMenuClick(props) {
  return function () {
    return props.toggleActivityMenuVisible();
  };
}
function getFeatureFlags(props) {
  var widgetSpace = props.widgetSpace,
    widgetStatus = props.widgetStatus;
  if (widgetStatus.get('hasFetchedFeatureFlags') || widgetStatus.get('isFetchingFeatureFlags')) {
    return;
  }
  props.updateWidgetStatus({
    isFetchingFeatureFlags: true
  });
  widgetSpace.get('activityTypes').toJS().forEach(function (activityType) {
    if (!activityType.feature) {
      return;
    }
    props.getFeature(activityType.feature.type, activityType.feature.key, props.sparkInstance);
  });
  props.updateWidgetStatus({
    hasFetchedFeatureFlags: true,
    isFetchingFeatureFlags: false
  });
}
var _default = exports.default = (0, _recompose.compose)((0, _reactRedux.connect)(function (state) {
  return state;
}, function (dispatch) {
  return (0, _redux.bindActionCreators)({
    getFeature: _reduxModuleFeatures.getFeature,
    updateWidgetStatus: _actions.updateWidgetStatus,
    updateActivityType: _actions.updateActivityType,
    storeActivityTypes: _actions.storeActivityTypes,
    toggleActivityMenuVisible: _actions.toggleActivityMenuVisible,
    updateSecondaryActivityType: _actions.updateSecondaryActivityType
  }, dispatch);
}), (0, _recompose.setPropTypes)(propTypes), (0, _recompose.withHandlers)({
  handleSecondaryDismiss: handleSecondaryDismiss,
  handleMenuClick: handleMenuClick,
  handleActivityChange: handleActivityChange,
  constructActivityWidgets: constructActivityWidgets,
  constructSecondaryActivityWidget: constructSecondaryActivityWidget
}), (0, _recompose.lifecycle)({
  componentWillMount: function componentWillMount() {
    var props = this.props;
    // Store activity types used in widget

    var DEFAULT_ACTIVITY_TYPES = [{
      displayName: 'Messages',
      name: 'message',
      buttonClassName: styles.messageButton,
      buttonType: 'chat',
      component: _widgetMessage.default,
      spaceTypes: ['group', 'direct'],
      type: ACTIVITY_TYPE_PRIMARY
    }, {
      displayName: 'Content',
      name: 'files',
      buttonClassName: styles.filesButton,
      buttonType: 'files',
      component: _widgetFiles.default,
      spaceTypes: ['direct', 'group'],
      type: ACTIVITY_TYPE_PRIMARY,
      props: {
        onClickClose: props.handleSecondaryDismiss,
        onClickMenu: props.handleMenuClick
      }
    }, {
      displayName: 'People',
      name: 'people',
      buttonClassName: styles.peopleButton,
      buttonType: {
        color: 'mint',
        icon: /*#__PURE__*/_react.default.createElement(_react2.Icon, {
          name: "icon-people_28"
        })
      },
      component: _widgetRoster.default,
      spaceTypes: ['direct', 'group'],
      type: ACTIVITY_TYPE_PRIMARY,
      props: {
        onClickClose: props.handleSecondaryDismiss,
        onClickMenu: props.handleMenuClick
      }
    }, {
      displayName: 'Call',
      name: 'meet',
      buttonClassName: styles.meetButton,
      buttonType: 'camera',
      component: _widgetMeet.default,
      spaceTypes: ['direct', 'group'],
      type: ACTIVITY_TYPE_PRIMARY
    }];
    var activityTypes = DEFAULT_ACTIVITY_TYPES;
    if ((0, _typeof2.default)(props.customActivityTypes) === 'object') {
      activityTypes = activityTypes.concat(props.customActivityTypes);
    }
    props.storeActivityTypes(activityTypes);
  },
  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    var activityTypes = nextProps.activityTypes,
      spaceDetails = nextProps.spaceDetails,
      sparkInstance = nextProps.sparkInstance,
      sparkState = nextProps.sparkState,
      initialActivity = nextProps.initialActivity,
      call = nextProps.call;
    var props = this.props;

    // Set the initial activity once we get space details
    if (sparkInstance && sparkState.get('authenticated') && sparkState.get('registered') && !sparkState.get('hasError')) {
      getFeatureFlags(nextProps);
    }
    if (!props.spaceDetails && spaceDetails) {
      if (activityTypes.find(function (a) {
        return a.name === initialActivity;
      })) {
        props.updateActivityType(initialActivity);
      }
      // if default activity is available
      else if (activityTypes.find(function (a) {
        return a.name === DEFAULT_ACTIVITY;
      })) {
        props.updateActivityType(DEFAULT_ACTIVITY);
      }
      // use first available activity in activity list
      else {
        props.updateActivityType(activityTypes[0].name);
      }
    }
    var prevCall = props.call;
    var hasMeet = activityTypes.some(function (activity) {
      return activity.name === 'meet';
    });
    var hasMessage = activityTypes.some(function (activity) {
      return activity.name === 'message';
    });

    // Switch to Meet if ringing
    if (hasMeet && call && !prevCall && !call.get('isDeclined')) {
      nextProps.updateActivityType('meet');
    }

    // Reset back to message view after call ends
    if (hasMessage && prevCall && !call) {
      nextProps.updateActivityType('message');
    }
  }
}));