import { extractLink, extractType } from '@atlaskit/link-extractors';
import { SmartLinkActionType } from '@atlaskit/linking-types';
import { CardAction } from '../../../constants';
import { getExtensionKey } from '../../../state/helpers';
import { canShowAction } from '../../../utils/actions/can-show-action';
import extractServerAction from '../extract-server-action';
var extractFollowAction = function extractFollowAction(response, actionOptions, id) {
  var _action$dataUpdateAct;
  if (!canShowAction(CardAction.FollowAction, actionOptions)) {
    return;
  }
  var extensionKey = getExtensionKey(response);
  var data = response === null || response === void 0 ? void 0 : response.data;
  var actions = extractServerAction(data);
  var type = 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 === SmartLinkActionType.FollowEntityAction || actionName === SmartLinkActionType.UnfollowEntityAction;
    }
    return false;
  });
  if (!action || !action.resourceIdentifiers) {
    return;
  }
  var url = 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 === SmartLinkActionType.FollowEntityAction,
    isProject: isProject
  };
};
export default extractFollowAction;