import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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 var extractInvokeViewAction = function extractInvokeViewAction(_ref, force) {
  var actionOptions = _ref.actionOptions,
    appearance = _ref.appearance,
    transformUrl = _ref.transformUrl,
    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() {
          var _transformUrl, destinationUrl;
          return _regeneratorRuntime.wrap(function (_context) {
            while (1) switch (_context.prev = _context.next) {
              case 0:
                if (!fg('platform_smartlink_xpc_url_wrapping')) {
                  _context.next = 1;
                  break;
                }
                destinationUrl = (_transformUrl = transformUrl === null || transformUrl === void 0 ? void 0 : transformUrl(url)) !== null && _transformUrl !== void 0 ? _transformUrl : url;
                return _context.abrupt("return", openUrl(destinationUrl));
              case 1:
                return _context.abrupt("return", openUrl(url));
              case 2:
              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)
    };
  }
};