"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractInvokeViewAction = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _constants = require("../../constants");
var _helpers = require("../../state/helpers");
var _utils = require("../../utils");
var _canShowAction = require("../../utils/actions/can-show-action");
var _extractActions = require("../common/actions/extractActions");
var extractInvokeViewAction = exports.extractInvokeViewAction = function extractInvokeViewAction(_ref, force) {
  var actionOptions = _ref.actionOptions,
    appearance = _ref.appearance,
    transformUrl = _ref.transformUrl,
    id = _ref.id,
    response = _ref.response;
  if (!(0, _canShowAction.canShowAction)(_constants.CardAction.ViewAction, actionOptions)) {
    return;
  }
  var data = response.data;
  if (!data) {
    return;
  }
  var url = (0, _linkExtractors.extractLink)(data);
  var viewActionExists = (0, _extractActions.getActionsFromJsonLd)(data).find(function (action) {
    return action['@type'] === 'ViewAction';
  });
  if (url && (viewActionExists || force)) {
    return {
      actionFn: function () {
        var _actionFn = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
          var _transformUrl, destinationUrl;
          return _regenerator.default.wrap(function (_context) {
            while (1) switch (_context.prev = _context.next) {
              case 0:
                if (!(0, _platformFeatureFlags.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", (0, _utils.openUrl)(destinationUrl));
              case 1:
                return _context.abrupt("return", (0, _utils.openUrl)(url));
              case 2:
              case "end":
                return _context.stop();
            }
          }, _callee);
        }));
        function actionFn() {
          return _actionFn.apply(this, arguments);
        }
        return actionFn;
      }(),
      actionSubjectId: 'shortcutGoToLink',
      actionType: 'ViewAction',
      definitionId: (0, _helpers.getDefinitionId)(response),
      display: appearance,
      extensionKey: (0, _helpers.getExtensionKey)(response),
      id: id,
      resourceType: (0, _helpers.getResourceType)(response)
    };
  }
};