"use strict";

var _Object$keys = require("@babel/runtime-corejs2/core-js/object/keys");
var _Object$getOwnPropertySymbols = require("@babel/runtime-corejs2/core-js/object/get-own-property-symbols");
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor");
var _Object$getOwnPropertyDescriptors = require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptors");
var _Object$defineProperties = require("@babel/runtime-corejs2/core-js/object/define-properties");
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.UPDATE_WIDGET_STATUS = exports.UPDATE_ACTIVITY_TYPE_SECONDARY = exports.UPDATE_ACTIVITY_TYPE = exports.UPDATE_ACTIVITY_MENU_VISIBLE = exports.TOGGLE_ACTIVITY_MENU_VISIBLE = exports.STORE_SPACE_DETAILS = exports.STORE_DESTINATION = exports.STORE_ACTIVITY_TYPES = exports.RELOAD_WIDGET = exports.FETCHING_SPACE_DETAILS = void 0;
exports.getSpaceDetails = getSpaceDetails;
exports.reloadWidget = reloadWidget;
exports.storeActivityTypes = storeActivityTypes;
exports.storeDestination = storeDestination;
exports.toggleActivityMenuVisible = toggleActivityMenuVisible;
exports.updateActivityMenuVisible = updateActivityMenuVisible;
exports.updateActivityType = updateActivityType;
exports.updateSecondaryActivityType = updateSecondaryActivityType;
exports.updateWidgetStatus = updateWidgetStatus;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));
var _reactComponentUtils = require("@webex/react-component-utils");
var _reduxModuleErrors = require("@webex/redux-module-errors");
var _messages = _interopRequireDefault(require("./messages"));
var _constants = require("./constants");
function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return _Object$getOwnPropertyDescriptor(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, _defineProperty2.default)(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
var FETCHING_SPACE_DETAILS = exports.FETCHING_SPACE_DETAILS = 'widget-space/FETCHING_SPACE_DETAILS';
var RELOAD_WIDGET = exports.RELOAD_WIDGET = 'widget-space/RELOAD_WIDGET';
var STORE_ACTIVITY_TYPES = exports.STORE_ACTIVITY_TYPES = 'widget-space/STORE_ACTIVITY_TYPES';
var STORE_DESTINATION = exports.STORE_DESTINATION = 'widget-space/STORE_DESTINATION';
var STORE_SPACE_DETAILS = exports.STORE_SPACE_DETAILS = 'widget-space/STORE_SPACE_DETAILS';
var TOGGLE_ACTIVITY_MENU_VISIBLE = exports.TOGGLE_ACTIVITY_MENU_VISIBLE = 'widget-space/TOGGLE_ACTIVITY_MENU_VISIBLE';
var UPDATE_ACTIVITY_TYPE = exports.UPDATE_ACTIVITY_TYPE = 'widget-space/UPDATE_ACTIVITY_TYPE';
var UPDATE_ACTIVITY_TYPE_SECONDARY = exports.UPDATE_ACTIVITY_TYPE_SECONDARY = 'widget-space/UPDATE_ACTIVITY_TYPE_SECONDARY';
var UPDATE_ACTIVITY_MENU_VISIBLE = exports.UPDATE_ACTIVITY_MENU_VISIBLE = 'widget-space/UPDATE_ACTIVITY_MENU_VISIBLE';
var UPDATE_WIDGET_STATUS = exports.UPDATE_WIDGET_STATUS = 'widget-space/UPDATE_WIDGET_STATUS';
function updateActivityType(type) {
  return {
    type: UPDATE_ACTIVITY_TYPE,
    payload: {
      type: type
    }
  };
}
function updateSecondaryActivityType(type) {
  return {
    type: UPDATE_ACTIVITY_TYPE_SECONDARY,
    payload: {
      type: type
    }
  };
}

/**
 * Resets the widget to "almost" default values
 * This is usually triggered when a destination changes
 *
 * @export
 * @returns {Object}
 */
function reloadWidget() {
  return {
    type: RELOAD_WIDGET
  };
}

/**
 * Stores the space widget's destination
 *
 * @export
 * @param {Object} {id, location}
 * @returns {Object}
 */
function storeDestination(_ref) {
  var id = _ref.id,
    cluster = _ref.cluster,
    type = _ref.type;
  return {
    type: STORE_DESTINATION,
    payload: _objectSpread({
      id: id,
      type: type
    }, cluster && {
      cluster: cluster
    })
  };
}
function updateActivityMenuVisible(isActivityMenuVisible) {
  return {
    type: UPDATE_ACTIVITY_MENU_VISIBLE,
    payload: {
      isActivityMenuVisible: isActivityMenuVisible
    }
  };
}
function toggleActivityMenuVisible() {
  return {
    type: TOGGLE_ACTIVITY_MENU_VISIBLE
  };
}
function storeSpaceDetails(details) {
  return {
    type: STORE_SPACE_DETAILS,
    payload: {
      details: details
    }
  };
}
function storeActivityTypes(activityTypes) {
  return {
    type: STORE_ACTIVITY_TYPES,
    payload: {
      activityTypes: activityTypes
    }
  };
}
function updateWidgetStatus(status) {
  return {
    type: UPDATE_WIDGET_STATUS,
    payload: {
      status: status
    }
  };
}
function fetchingSpaceDetails() {
  return {
    type: FETCHING_SPACE_DETAILS
  };
}

/**
 * Gets details about the space
 * @param {Object} options.sparkInstance
 * @param {String} options.destinationId
 * @param {String} options.destinationType
 * @param {String} options.destinationCluster
 * @param {Object} options.intl
 * @returns {Thunk}
 */
function getSpaceDetails(_ref2) {
  var sparkInstance = _ref2.sparkInstance,
    destinationId = _ref2.destinationId,
    destinationType = _ref2.destinationType,
    intl = _ref2.intl;
  return function (dispatch) {
    // We cannot fetch space details for 1:1's here, it will be handled in conversation store
    if ([_constants.destinationTypes.EMAIL, _constants.destinationTypes.USERID, _constants.destinationTypes.SIP, _constants.destinationTypes.PSTN].includes(destinationType)) {
      return dispatch(storeSpaceDetails({
        type: 'direct'
      }));
    }
    var formatMessage = intl.formatMessage;
    var _validateAndDecodeId = (0, _reactComponentUtils.validateAndDecodeId)(destinationId),
      spaceIdUUID = _validateAndDecodeId.id;
    if (!spaceIdUUID) {
      var displayTitle = formatMessage(_messages.default.unableToLoad);
      var displaySubtitle = formatMessage(_messages.default.badSpaceId);
      return dispatch((0, _reduxModuleErrors.addError)({
        id: 'getSpace.badId',
        displayTitle: displayTitle,
        displaySubtitle: displaySubtitle,
        temporary: false
      }));
    }
    dispatch(fetchingSpaceDetails());
    return sparkInstance.request({
      service: 'hydra',
      resource: "/rooms/".concat(destinationId)
    }).then(function (res) {
      return dispatch(storeSpaceDetails(res.body));
    }).catch(function (error) {
      var displayTitle = formatMessage(_messages.default.unableToLoad);
      var displaySubtitle;
      if (error.statusCode === 401) {
        displaySubtitle = formatMessage(_messages.default.errorBadToken);
      } else if (error.statusCode === 404) {
        displaySubtitle = formatMessage(_messages.default.errorNotFound);
      }
      return dispatch((0, _reduxModuleErrors.addError)({
        id: 'getSpace.hydraFail',
        displayTitle: displayTitle,
        displaySubtitle: displaySubtitle,
        temporary: false,
        code: error.statusCode
      }));
    });
  };
}