import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["value", "isProject"];
import React from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import FeatureGates from '@atlaskit/feature-gate-js-client';
import { fg } from '@atlaskit/platform-feature-flags';
import { ActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import { importIcon } from '../../utils';
import ServerAction from '../action/server-action';
import { getFollowActionErrorMessage } from './utils';
var importIconMapper = {
  goal: function goal() {
    return import( /* webpackChunkName: "glyphGoal" */'./goal-icon').then(function (_ref) {
      var GoalIcon = _ref.GoalIcon;
      return {
        default: GoalIcon
      };
    });
  },
  projects: function projects() {
    return import( /* 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 importIcon(importFn);
};
var FollowAction = function FollowAction(props) {
  var _context$actions;
  var context = useFlexibleUiContext();
  var intl = useIntl();
  var actionData = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.FollowAction];
  if (!context || !actionData) {
    return null;
  }
  var value = actionData.value,
    isProject = actionData.isProject,
    data = _objectWithoutProperties(actionData, _excluded);
  var isStackItem = props.as === 'stack-item';
  var message = value ? messages.follow : messages.unfollow;
  var projectMessage = value ? messages.follow_project : messages.unfollow_project;
  var goalMessage = value ? messages.follow_goal : messages.unfollow_goal;
  var stackMessage = isProject ? projectMessage : goalMessage;
  var label = isStackItem ? stackMessage : message;
  var projectTooltipMessage = value ? FeatureGates.getExperimentValue('project-terminology-refresh', 'isEnabled', false) ? messages.follow_project_descriptionGalaxia : messages.follow_project_description : FeatureGates.getExperimentValue('project-terminology-refresh', 'isEnabled', false) ? messages.unfollow_project_descriptionGalaxia : messages.unfollow_project_description;
  var goalTooltipMessage = value ? messages.follow_goal_description : 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.createElement(ImportedIcon, {
    label: "Follow",
    testId: "smart-action-follow-action-".concat(stackIconType, "-icon")
  });
  var handleError = function handleError() {
    var onErrorCallback = props.onError;
    var errorMessage = getFollowActionErrorMessage(isProject, value);
    onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback({
      title: /*#__PURE__*/React.createElement(FormattedMessage, errorMessage),
      appearance: 'error'
    });
  };
  return /*#__PURE__*/React.createElement(ServerAction, _extends({
    content: /*#__PURE__*/React.createElement(FormattedMessage, label)
  }, fg('platform_sl_action_refactoring') ? {
    ariaLabel: intl.formatMessage(label)
  } : {}, {
    icon: followIcon,
    testId: "smart-action-follow-action",
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, tooltipMessage)
  }, data, props, {
    onError: handleError
  }));
};
export default FollowAction;