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';
const extractFollowAction = (response, actionOptions, id) => {
  var _action$dataUpdateAct;
  if (!canShowAction(CardAction.FollowAction, actionOptions)) {
    return;
  }
  const extensionKey = getExtensionKey(response);
  const data = response === null || response === void 0 ? void 0 : response.data;
  const actions = extractServerAction(data);
  const type = extractType(data);
  const isProject = type === null || type === void 0 ? void 0 : type.includes('atlassian:Project');
  if (!extensionKey || actions.length === 0) {
    return;
  }
  const action = actions.find(item => {
    if ((item === null || item === void 0 ? void 0 : item.name) === 'UpdateAction') {
      var _dataUpdateAction;
      const actionName = item === null || item === void 0 ? 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;
  }
  const url = extractLink(data);
  const reload = url ? {
    id,
    url
  } : undefined;
  const actionType = (_action$dataUpdateAct = action.dataUpdateAction) === null || _action$dataUpdateAct === void 0 ? void 0 : _action$dataUpdateAct.name;
  return {
    action: {
      action: {
        actionType,
        resourceIdentifiers: action.resourceIdentifiers
      },
      providerKey: extensionKey,
      reload
    },
    value: actionType === SmartLinkActionType.FollowEntityAction,
    isProject
  };
};
export default extractFollowAction;