"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.extractInvokePreviewAction = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _linkExtractors = require("@atlaskit/link-extractors");
var _utils = require("@atlaskit/linking-common/utils");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _constants = require("../../constants");
var _index = require("../../index");
var _helpers = require("../../state/helpers");
var _canShowAction = require("../../utils/actions/can-show-action");
var _utils2 = require("../../view/EmbedModal/utils");
var _extractIsSupportTheming = require("../common/meta/extractIsSupportTheming");
var _extractIsTrusted = require("../common/meta/extractIsTrusted");
var _icon = require("../flexible/icon");
var _extractInvokeDownloadAction = require("./extract-invoke-download-action");
var _extractInvokeViewAction = require("./extract-invoke-view-action");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var extractInvokePreviewAction = exports.extractInvokePreviewAction = function extractInvokePreviewAction(param) {
  var _extractPreviewData;
  var actionOptions = param.actionOptions,
    display = param.appearance,
    fireEvent = param.fireEvent,
    onClose = param.onClose,
    id = param.id,
    origin = param.origin,
    response = param.response,
    isPreviewPanelAvailable = param.isPreviewPanelAvailable,
    openPreviewPanel = param.openPreviewPanel;
  var ari = (0, _linkExtractors.extractSmartLinkAri)(response);
  var url = (0, _linkExtractors.extractSmartLinkUrl)(response);
  var name = (0, _linkExtractors.extractSmartLinkTitle)(response);
  var hasPreviewPanelParams = Boolean((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && isPreviewPanelAvailable && openPreviewPanel && ari && url && name);
  var hasPreviewPanel = hasPreviewPanelParams && isPreviewPanelAvailable({
    ari: ari
  });
  var isInPreviewPanel = (0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && (0, _utils.isWithinPreviewPanelIFrame)();
  var data = response.data;
  var meta = response.meta;
  if (!(0, _canShowAction.canShowAction)(_constants.CardAction.PreviewAction, actionOptions)) {
    return;
  }

  /* Analytics UI event for preview action created to support tracking of preview actions as the experiment rolls out */
  var firePreviewActionUIEvent = function firePreviewActionUIEvent(_ref) {
    var previewType = _ref.previewType;
    if (!(0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') || origin !== 'smartLinkPreviewHoverCard' || !fireEvent) {
      return;
    }
    fireEvent('ui.smartLink.clicked.previewHoverCard', {
      id: id || '',
      display: 'hoverCardPreview',
      previewType: previewType
    });
  };
  var src = (_extractPreviewData = (0, _linkExtractors.extractPreview)(data, 'web')) === null || _extractPreviewData === void 0 ? void 0 : _extractPreviewData.src;

  // disallow preview modals within preview panels
  if (!hasPreviewPanel && isInPreviewPanel) {
    return;
  }
  if (src) {
    var extensionKey = (0, _helpers.getExtensionKey)(response);
    return {
      hasPreviewPanel: hasPreviewPanel,
      invokeAction: {
        actionFn: function () {
          var _actionFn = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
            var _extractSmartLinkEmbe, resolvedUrl, responseUrl, _extractSmartLinkProv, _extractSmartLinkEmbe2, _actionOptions$previe, _actionOptions$previe2;
            return _regenerator.default.wrap(function (_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  if (!hasPreviewPanel) {
                    _context.next = 1;
                    break;
                  }
                  resolvedUrl = url;
                  if ((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test')) {
                    responseUrl = response.data && 'url' in response.data ? response.data.url : undefined;
                    resolvedUrl = responseUrl || url;
                  }
                  openPreviewPanel({
                    ari: ari,
                    url: resolvedUrl,
                    name: name,
                    iconUrl: undefined,
                    panelData: {
                      embedUrl: (0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') ? (_extractSmartLinkEmbe = (0, _linkExtractors.extractSmartLinkEmbed)(response)) === null || _extractSmartLinkEmbe === void 0 ? void 0 : _extractSmartLinkEmbe.src : undefined
                    }
                  });
                  (0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && firePreviewActionUIEvent({
                    previewType: 'panel'
                  });
                  _context.next = 3;
                  break;
                case 1:
                  _context.next = 2;
                  return (0, _utils2.openEmbedModal)(_objectSpread({
                    fireEvent: fireEvent,
                    extensionKey: extensionKey,
                    id: id,
                    invokeDownloadAction: (0, _extractInvokeDownloadAction.extractInvokeDownloadAction)(param),
                    invokeViewAction: (0, _extractInvokeViewAction.extractInvokeViewAction)(param, true),
                    isSupportTheming: (0, _extractIsSupportTheming.extractIsSupportTheming)(meta),
                    isTrusted: (0, _extractIsTrusted.extractIsTrusted)(meta),
                    linkIcon: (0, _icon.extractLinkIcon)(response),
                    providerName: (_extractSmartLinkProv = (0, _linkExtractors.extractSmartLinkProvider)(response)) === null || _extractSmartLinkProv === void 0 ? void 0 : _extractSmartLinkProv.text,
                    onClose: onClose,
                    origin: origin,
                    src: (_extractSmartLinkEmbe2 = (0, _linkExtractors.extractSmartLinkEmbed)(response)) === null || _extractSmartLinkEmbe2 === void 0 ? void 0 : _extractSmartLinkEmbe2.src,
                    title: (0, _linkExtractors.extractSmartLinkTitle)(response),
                    url: url,
                    size: actionOptions === null || actionOptions === void 0 || (_actionOptions$previe = actionOptions.previewAction) === null || _actionOptions$previe === void 0 ? void 0 : _actionOptions$previe.size,
                    isBlanketHidden: actionOptions === null || actionOptions === void 0 || (_actionOptions$previe2 = actionOptions.previewAction) === null || _actionOptions$previe2 === void 0 ? void 0 : _actionOptions$previe2.hideBlanket
                  }, (0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && {
                    isInPreviewPanel: isInPreviewPanel
                  }));
                case 2:
                  (0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && firePreviewActionUIEvent({
                    previewType: 'modal'
                  });
                case 3:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          }));
          function actionFn() {
            return _actionFn.apply(this, arguments);
          }
          return actionFn;
        }(),
        actionSubjectId: 'invokePreviewScreen',
        actionType: _index.ActionName.PreviewAction,
        display: display,
        extensionKey: extensionKey,
        id: id
      }
    };
  }
};