"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _linkExtractors = require("@atlaskit/link-extractors");
var _linkingTypes = require("@atlaskit/linking-types");
var _constants = require("../../../constants");
var _helpers = require("../../../state/helpers");
var _canShowAction = require("../../../utils/actions/can-show-action");
var _extractServerAction = _interopRequireDefault(require("../extract-server-action"));
var extractFollowAction = function extractFollowAction(response, actionOptions, id) {
  var _action$dataUpdateAct;
  if (!(0, _canShowAction.canShowAction)(_constants.CardAction.FollowAction, actionOptions)) {
    return;
  }
  var extensionKey = (0, _helpers.getExtensionKey)(response);
  var data = response === null || response === void 0 ? void 0 : response.data;
  var actions = (0, _extractServerAction.default)(data);
  var type = (0, _linkExtractors.extractType)(data);
  var isProject = type === null || type === void 0 ? void 0 : type.includes('atlassian:Project');
  if (!extensionKey || actions.length === 0) {
    return;
  }
  var action = actions.find(function (item) {
    if ((item === null || item === void 0 ? void 0 : item.name) === 'UpdateAction') {
      var _dataUpdateAction;
      var actionName = item === null || item === void 0 || (_dataUpdateAction = item.dataUpdateAction) === null || _dataUpdateAction === void 0 ? void 0 : _dataUpdateAction.name;
      return actionName === _linkingTypes.SmartLinkActionType.FollowEntityAction || actionName === _linkingTypes.SmartLinkActionType.UnfollowEntityAction;
    }
    return false;
  });
  if (!action || !action.resourceIdentifiers) {
    return;
  }
  var url = (0, _linkExtractors.extractLink)(data);
  var reload = url ? {
    id: id,
    url: url
  } : undefined;
  var actionType = (_action$dataUpdateAct = action.dataUpdateAction) === null || _action$dataUpdateAct === void 0 ? void 0 : _action$dataUpdateAct.name;
  return {
    action: {
      action: {
        actionType: actionType,
        resourceIdentifiers: action.resourceIdentifiers
      },
      providerKey: extensionKey,
      reload: reload
    },
    value: actionType === _linkingTypes.SmartLinkActionType.FollowEntityAction,
    isProject: isProject
  };
};
var _default = exports.default = extractFollowAction;