import { extractLink } from '@atlaskit/link-extractors';
import { fg } from '@atlaskit/platform-feature-flags';
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,
  transformUrl,
  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 () => {
        if (fg('platform_smartlink_xpc_url_wrapping')) {
          var _transformUrl;
          const destinationUrl = (_transformUrl = transformUrl === null || transformUrl === void 0 ? void 0 : transformUrl(url)) !== null && _transformUrl !== void 0 ? _transformUrl : url;
          return openUrl(destinationUrl);
        }
        return openUrl(url);
      },
      actionSubjectId: 'shortcutGoToLink',
      actionType: 'ViewAction',
      definitionId: getDefinitionId(response),
      display: appearance,
      extensionKey: getExtensionKey(response),
      id,
      resourceType: getResourceType(response)
    };
  }
};