"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _reactIntlNext = require("react-intl-next");
var _featureGateJsClient = _interopRequireDefault(require("@atlaskit/feature-gate-js-client"));
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _utils = require("../../utils");
var _serverAction = _interopRequireDefault(require("../action/server-action"));
var _utils2 = require("./utils");
var _excluded = ["value", "isProject"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != (0, _typeof2.default)(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var importIconMapper = {
  goal: function goal() {
    return Promise.resolve().then(function () {
      return _interopRequireWildcard(require( /* webpackChunkName: "glyphGoal" */'./goal-icon'));
    }).then(function (_ref) {
      var GoalIcon = _ref.GoalIcon;
      return {
        default: GoalIcon
      };
    });
  },
  projects: function projects() {
    return Promise.resolve().then(function () {
      return _interopRequireWildcard(require( /* webpackChunkName: "glyphProjects" */'./projects-icon'));
    }).then(function (_ref2) {
      var ProjectsIcon = _ref2.ProjectsIcon;
      return {
        default: ProjectsIcon
      };
    });
  }
};
var getIcon = function getIcon(stackIconType) {
  var importFn = importIconMapper[stackIconType];
  if (!importFn) {
    return null;
  }
  return (0, _utils.importIcon)(importFn);
};
var FollowAction = function FollowAction(props) {
  var _context$actions;
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var actionData = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.FollowAction];
  if (!context || !actionData) {
    return null;
  }
  var value = actionData.value,
    isProject = actionData.isProject,
    data = (0, _objectWithoutProperties2.default)(actionData, _excluded);
  var isStackItem = props.as === 'stack-item';
  var message = value ? _messages.messages.follow : _messages.messages.unfollow;
  var projectMessage = value ? _messages.messages.follow_project : _messages.messages.unfollow_project;
  var goalMessage = value ? _messages.messages.follow_goal : _messages.messages.unfollow_goal;
  var stackMessage = isProject ? projectMessage : goalMessage;
  var label = isStackItem ? stackMessage : message;
  var projectTooltipMessage = value ? _featureGateJsClient.default.getExperimentValue('project-terminology-refresh', 'isEnabled', false) ? _messages.messages.follow_project_descriptionGalaxia : _messages.messages.follow_project_description : _featureGateJsClient.default.getExperimentValue('project-terminology-refresh', 'isEnabled', false) ? _messages.messages.unfollow_project_descriptionGalaxia : _messages.messages.unfollow_project_description;
  var goalTooltipMessage = value ? _messages.messages.follow_goal_description : _messages.messages.unfollow_goal_description;
  var stackTooltipMessage = isProject ? projectTooltipMessage : goalTooltipMessage;
  var tooltipMessage = isStackItem ? stackTooltipMessage : message;
  var stackIconType = isProject ? 'projects' : 'goal';
  var ImportedIcon = getIcon(stackIconType);
  var followIcon = /*#__PURE__*/_react.default.createElement(ImportedIcon, {
    label: "Follow",
    testId: "smart-action-follow-action-".concat(stackIconType, "-icon")
  });
  var handleError = function handleError() {
    var onErrorCallback = props.onError;
    var errorMessage = (0, _utils2.getFollowActionErrorMessage)(isProject, value);
    onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback({
      title: /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, errorMessage),
      appearance: 'error'
    });
  };
  return /*#__PURE__*/_react.default.createElement(_serverAction.default, (0, _extends2.default)({
    content: /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, label),
    icon: followIcon,
    testId: "smart-action-follow-action",
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, tooltipMessage)
  }, data, props, {
    onError: handleError
  }));
};
var _default = exports.default = FollowAction;