import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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 var extractInvokeViewAction = function extractInvokeViewAction(_ref, force) {
  var actionOptions = _ref.actionOptions,
    appearance = _ref.appearance,
    id = _ref.id,
    response = _ref.response;
  if (!canShowAction(CardAction.ViewAction, actionOptions)) {
    return;
  }
  var data = response.data;
  if (!data) {
    return;
  }
  var url = extractLink(data);
  var viewActionExists = getActionsFromJsonLd(data).find(function (action) {
    return action['@type'] === 'ViewAction';
  });
  if (url && (viewActionExists || force)) {
    return {
      actionFn: function () {
        var _actionFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          return _regeneratorRuntime.wrap(function (_context) {
            while (1) switch (_context.prev = _context.next) {
              case 0:
                return _context.abrupt("return", openUrl(url));
              case 1:
              case "end":
                return _context.stop();
            }
          }, _callee);
        }));
        function actionFn() {
          return _actionFn.apply(this, arguments);
        }
        return actionFn;
      }(),
      actionSubjectId: 'shortcutGoToLink',
      actionType: 'ViewAction',
      definitionId: getDefinitionId(response),
      display: appearance,
      extensionKey: getExtensionKey(response),
      id: id,
      resourceType: getResourceType(response)
    };
  }
};