"use strict";

var _typeof = require("@babel/runtime-corejs2/helpers/typeof");
var _Reflect$construct = require("@babel/runtime-corejs2/core-js/reflect/construct");
var _WeakMap = require("@babel/runtime-corejs2/core-js/weak-map");
var _Object$defineProperty2 = require("@babel/runtime-corejs2/core-js/object/define-property");
var _Object$getOwnPropertyDescriptor2 = require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty2(exports, "__esModule", {
  value: true
});
exports.ownPropTypes = exports.default = exports.SpaceWidget = void 0;
var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-symbols"));
var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor"));
var _getOwnPropertyDescriptors = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptors"));
var _defineProperties = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/define-properties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/define-property"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/toConsumableArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/getPrototypeOf"));
var _defineProperty3 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _reactTransformHmr3 = _interopRequireDefault(require("react-transform-hmr"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactRedux = require("react-redux");
var _redux = require("redux");
var _classnames = _interopRequireDefault(require("classnames"));
var _recompose = require("recompose");
var _react2 = require("@momentum-ui/react");
var _reactComponentTitleBar = _interopRequireDefault(require("@webex/react-component-title-bar"));
var _reactComponentLoadingScreen = _interopRequireDefault(require("@webex/react-component-loading-screen"));
var _reactComponentTimer = _interopRequireDefault(require("@webex/react-component-timer"));
var _reactComponentErrorDisplay = _interopRequireDefault(require("@webex/react-component-error-display"));
var _actions = require("../../react-redux-spark/src/actions");
var _activityMenu = _interopRequireDefault(require("./components/activity-menu"));
var _actions2 = require("./actions");
var _messages = _interopRequireDefault(require("./messages"));
require("./momentum.scss");
var _enhancers = _interopRequireDefault(require("./enhancers"));
var _activityMenu2 = require("./enhancers/activity-menu");
function _getRequireWildcardCache(e) { if ("function" != typeof _WeakMap) return null; var r = new _WeakMap(), t = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = _Object$defineProperty2 && _Object$getOwnPropertyDescriptor2; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? _Object$getOwnPropertyDescriptor2(e, u) : null; i && (i.get || i.set) ? _Object$defineProperty2(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function ownKeys(e, r) { var t = (0, _keys.default)(e); if (_getOwnPropertySymbols.default) { var o = (0, _getOwnPropertySymbols.default)(e); r && (o = o.filter(function (r) { return (0, _getOwnPropertyDescriptor.default)(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty3.default)(e, r, t[r]); }) : _getOwnPropertyDescriptors.default ? (0, _defineProperties.default)(e, (0, _getOwnPropertyDescriptors.default)(t)) : ownKeys(Object(t)).forEach(function (r) { (0, _defineProperty2.default)(e, r, (0, _getOwnPropertyDescriptor.default)(t, r)); }); } return e; }
var _components = {
  SpaceWidget: {
    displayName: "SpaceWidget"
  }
};
var _reactTransformHmr2 = (0, _reactTransformHmr3.default)({
  filename: "packages/node_modules/@webex/widget-space/src/container.js",
  components: _components,
  locals: [module],
  imports: [_react.default]
});
function _wrapComponent(id) {
  return function (Component) {
    return _reactTransformHmr2(Component, id);
  };
}
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 injectedPropTypes = {
  activityTypes: _propTypes.default.array.isRequired,
  call: _propTypes.default.object,
  conversation: _propTypes.default.object.isRequired,
  currentActivity: _propTypes.default.string,
  errors: _propTypes.default.object.isRequired,
  sparkInstance: _propTypes.default.object,
  widgetSpace: _propTypes.default.object.isRequired
};
var ownPropTypes = exports.ownPropTypes = _objectSpread(_objectSpread({
  composerActions: _propTypes.default.shape({
    attachFiles: _propTypes.default.bool
  }),
  customActivityTypes: _propTypes.default.object,
  destinationId: _propTypes.default.string,
  destinationType: _propTypes.default.oneOf(['email', 'userId', 'spaceId', 'sip', 'pstn']),
  muteNotifications: _propTypes.default.bool,
  secondaryActivitiesFullWidth: _propTypes.default.bool,
  setCurrentActivity: _propTypes.default.string,
  spaceActivities: _propTypes.default.shape({
    files: _propTypes.default.bool,
    meet: _propTypes.default.bool,
    message: _propTypes.default.bool,
    people: _propTypes.default.bool
  }),
  startCall: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.bool]),
  disablePresence: _propTypes.default.bool,
  disableFlags: _propTypes.default.bool,
  showSubmitButton: _propTypes.default.bool,
  sendMessageOnReturnKey: _propTypes.default.bool
}, _activityMenu2.propTypes), injectedPropTypes);
var defaultProps = {
  composerActions: {
    attachFiles: true
  },
  customActivityTypes: undefined,
  destinationId: null,
  destinationType: null,
  muteNotifications: false,
  secondaryActivitiesFullWidth: true,
  setCurrentActivity: '',
  spaceActivities: {
    files: true,
    meet: true,
    message: true,
    people: true
  },
  startCall: false,
  disablePresence: false,
  disableFlags: false,
  showSubmitButton: false,
  sendMessageOnReturnKey: true
};
var SpaceWidget = exports.SpaceWidget = /*#__PURE__*/function (_Component) {
  (0, _inherits2.default)(SpaceWidget, _Component);
  var _super = _createSuper(SpaceWidget);
  function SpaceWidget() {
    var _this;
    (0, _classCallCheck2.default)(this, SpaceWidget);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty3.default)((0, _assertThisInitialized2.default)(_this), "handleTabKeyDown", function (e, index, visibleTabs) {
      var key = e.key;
      var allTabs = document.querySelectorAll('[role="tab"]');
      var meetButton = document.querySelector('.webex-tab-meet button'); // meet button element

      switch (key) {
        case 'ArrowRight':
          {
            e.preventDefault();

            // If last tab (People) → go to Meet button
            if (index === visibleTabs.length - 1 && meetButton) {
              meetButton.focus();
            } else {
              var next = (index + 1) % visibleTabs.length;
              allTabs[next].focus();
            }
            break;
          }
        case 'ArrowLeft':
          {
            e.preventDefault();

            // If focus is currently on Meet button → move to last tab (People)
            if (document.activeElement === meetButton) {
              allTabs[visibleTabs.length - 1].focus();
            } else {
              var prev = (index - 1 + visibleTabs.length) % visibleTabs.length;
              allTabs[prev].focus();
            }
            break;
          }
        case 'Home':
          {
            e.preventDefault();
            allTabs[0].focus();
            break;
          }
        case 'End':
          {
            e.preventDefault();
            // End should go to Meet button if present, else last tab
            if (meetButton) {
              meetButton.focus();
            } else {
              allTabs[visibleTabs.length - 1].focus();
            }
            break;
          }
        case ' ':
        case 'Enter':
          {
            e.preventDefault();
            _this.props.handleActivityChange(visibleTabs[index]);
            break;
          }
        default:
          break;
      }
    });
    (0, _defineProperty3.default)((0, _assertThisInitialized2.default)(_this), "handleMeetKeyDown", function (e) {
      var allTabs = document.querySelectorAll('[role="tab"]');
      switch (e.key) {
        case 'ArrowLeft':
          {
            e.preventDefault();
            // Move focus back to last tab (People)
            allTabs[allTabs.length - 1].focus();
            break;
          }
        case 'Home':
          {
            e.preventDefault();
            allTabs[0].focus();
            break;
          }
        case 'End':
          {
            e.preventDefault();
            // Stay on Meet (already focused)
            e.currentTarget.focus();
            break;
          }
        default:
          break;
      }
    });
    return _this;
  }
  (0, _createClass2.default)(SpaceWidget, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var sparkInstance = this.props.sparkInstance;

      // Make sure we unregister device when unmouting this component
      this.props.unregisterDevice(sparkInstance);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;
      var props = this.props;
      var activityTypes = props.activityTypes,
        call = props.call,
        conversation = props.conversation,
        currentActivity = props.currentActivity,
        composerActions = props.composerActions,
        sparkInstance = props.sparkInstance,
        spaceDetails = props.spaceDetails,
        errors = props.errors,
        widgetSpace = props.widgetSpace,
        widgetStatus = props.widgetStatus;
      var formatMessage = props.intl.formatMessage;
      var errorElement;
      if (errors.get('hasError') || conversation.getIn(['status', 'error'])) {
        var widgetError = errors.get('errors').first();
        if (!widgetError) {
          widgetError = {
            displaySubtitle: conversation.getIn(['status', 'error', 'description']),
            temporary: false,
            title: formatMessage(_messages.default.errorConversation)
          };
        }
        errorElement = /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-error-wrapper', styles.errorWrapper)
        }, /*#__PURE__*/_react.default.createElement(_reactComponentErrorDisplay.default, (0, _extends2.default)({
          secondaryTitle: widgetError.displaySubtitle,
          title: widgetError.displayTitle,
          transparent: widgetError.temporary
        }, widgetError)));
      }
      if (sparkInstance && spaceDetails) {
        // Construct widgets
        var primaryActivityType = widgetSpace.get('primaryActivityType');
        var secondaryActivityType = widgetSpace.get('secondaryActivityType');
        var widgets = props.constructActivityWidgets(activityTypes, primaryActivityType, composerActions);
        var secondaryWidget = props.constructSecondaryActivityWidget(activityTypes, secondaryActivityType);
        var avatarId = spaceDetails.avatarId,
          _spaceDetails$title = spaceDetails.title,
          title = _spaceDetails$title === void 0 ? 'Loading...' : _spaceDetails$title;
        var callStartTime = (call === null || call === void 0 ? void 0 : call.activeParticipantsCount) > 0 ? call === null || call === void 0 ? void 0 : call.startTime : null;
        var menuButton;
        var preferredWebexSite = sparkInstance.meetings.preferredWebexSite;
        var isMeetButtonDisabled = false;
        if (props.destinationType === 'spaceId' && activityTypes.some(function (activity) {
          return activity.name === 'meet';
        })) {
          isMeetButtonDisabled = !preferredWebexSite;
        }
        var errorElementSpace = /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-error-wrapper', styles.errorWrapper)
        }, /*#__PURE__*/_react.default.createElement(_reactComponentErrorDisplay.default, {
          transparent: false,
          title: formatMessage(_messages.default.noAbilityToStartMeeting)
        }));
        if (activityTypes) {
          menuButton = [];
          var allTabs = activityTypes.filter(function (at) {
            return at.name;
          });
          var visibleTabs = activityTypes.filter(function (at) {
            return at.name !== 'meet';
          });
          var totalTabs = visibleTabs.length;
          activityTypes.forEach(function (at) {
            var tabActive = currentActivity === at.name;
            if (at.name === 'meet') {
              menuButton.push( /*#__PURE__*/_react.default.createElement("span", {
                key: at.name,
                className: (0, _classnames.default)("webex-tab-".concat(at.name), styles.tabMeet),
                title: isMeetButtonDisabled ? formatMessage(_messages.default.noAbilityToStartMeeting) : ''
              }, /*#__PURE__*/_react.default.createElement(_react2.Button, {
                role: "button",
                ariaLabel: at.displayName,
                circle: !call,
                id: styles.huddle,
                color: "green",
                size: 25,
                disabled: isMeetButtonDisabled,
                onClick: function onClick() {
                  return _this2.props.handleActivityChange(at);
                },
                onKeyDown: _this2.handleMeetKeyDown
              }, /*#__PURE__*/_react.default.createElement(_react2.Icon, {
                name: "icon-".concat(at.buttonType, "_16"),
                style: {
                  paddingRight: callStartTime ? '0.5rem' : 'inherit'
                }
              }), callStartTime && /*#__PURE__*/_react.default.createElement(_reactComponentTimer.default, {
                startTime: callStartTime
              }))));
            } else {
              var tabIndexInList = visibleTabs.findIndex(function (vt) {
                return vt.name === at.name;
              });
              menuButton.push( /*#__PURE__*/_react.default.createElement("button", {
                key: at.name,
                role: "tab",
                "aria-selected": tabActive,
                "aria-posinset": tabIndexInList + 1,
                "aria-setsize": totalTabs,
                tabIndex: 0,
                "aria-label": at.displayName,
                onClick: function onClick() {
                  return _this2.props.handleActivityChange(at);
                },
                onKeyDown: function onKeyDown(e) {
                  return _this2.handleTabKeyDown(e, tabIndexInList, visibleTabs);
                },
                className: (0, _classnames.default)("webex-tab-".concat(at.name), styles.tabOther, (0, _defineProperty3.default)({}, styles.tabActive, tabActive))
              }, at.displayName));
            }
          });
        }
        return /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-space-widget', 'md', styles.spaceWidget)
        }, errorElement, widgetStatus.get('activityMenuVisible') && /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-activity-menu-wrapper', styles.activityMenuWrapper)
        }, /*#__PURE__*/_react.default.createElement(_activityMenu.default, {
          activityTypes: activityTypes,
          onChange: props.handleActivityChange,
          onExit: props.handleMenuClick,
          showExitButton: true
        })), secondaryWidget, /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-title-bar-wrapper', styles.titleBarWrapper)
        }, /*#__PURE__*/_react.default.createElement(_reactComponentTitleBar.default, {
          avatarId: avatarId,
          name: title,
          type: spaceDetails.type
        }, callStartTime && /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-title-bar-call-timer', styles.callTimer)
        }, /*#__PURE__*/_react.default.createElement(_reactComponentTimer.default, {
          startTime: callStartTime
        })))), /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-tabs', styles.tabContainer),
          role: "tablist",
          "aria-label": "Space Widget"
        }, menuButton), /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('webex-widget-body', styles.widgetBody)
        }, isMeetButtonDisabled && currentActivity === 'meet' ? errorElementSpace : widgets));
      }
      if (errors.get('hasError')) {
        return errorElement;
      }
      return /*#__PURE__*/_react.default.createElement(_reactComponentLoadingScreen.default, null);
    }
  }]);
  return SpaceWidget;
}(_react.Component);
exports.SpaceWidget = SpaceWidget = _wrapComponent("SpaceWidget")(SpaceWidget);
SpaceWidget.propTypes = ownPropTypes;
SpaceWidget.defaultProps = defaultProps;
var _default2 = exports.default = _recompose.compose.apply(void 0, [(0, _reactRedux.connect)(null, function (dispatch) {
  return (0, _redux.bindActionCreators)({
    storeDestination: _actions2.storeDestination,
    unregisterDevice: _actions.unregisterDevice
  }, dispatch);
})].concat((0, _toConsumableArray2.default)(_enhancers.default)))(SpaceWidget);