import { extractLink } from '@atlaskit/link-extractors';
import { CardAction } from '../../constants';
import { getDefinitionId, getExtensionKey, getResourceType } from '../../state/helpers';
import { openUrl } from '../../utils';
import { canShowAction } from '../../utils/actions/can-show-action';
import { getActionsFromJsonLd } from '../common/actions/extractActions';
export const extractInvokeViewAction = ({
  actionOptions,
  appearance,
  id,
  response
}, force) => {
  if (!canShowAction(CardAction.ViewAction, actionOptions)) {
    return;
  }
  const data = response.data;
  if (!data) {
    return;
  }
  const url = extractLink(data);
  const viewActionExists = getActionsFromJsonLd(data).find(action => action['@type'] === 'ViewAction');
  if (url && (viewActionExists || force)) {
    return {
      actionFn: async () => openUrl(url),
      actionSubjectId: 'shortcutGoToLink',
      actionType: 'ViewAction',
      definitionId: getDefinitionId(response),
      display: appearance,
      extensionKey: getExtensionKey(response),
      id,
      resourceType: getResourceType(response)
    };
  }
};