import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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) { _defineProperty(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; }
import { extractPreview as extractPreviewData, extractSmartLinkAri, extractSmartLinkEmbed, extractSmartLinkProvider, extractSmartLinkTitle, extractSmartLinkUrl } from '@atlaskit/link-extractors';
import { isWithinPreviewPanelIFrame } from '@atlaskit/linking-common/utils';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { CardAction } from '../../constants';
import { ActionName } from '../../index';
import { getExtensionKey } from '../../state/helpers';
import { canShowAction } from '../../utils/actions/can-show-action';
import { openEmbedModal } from '../../view/EmbedModal/utils';
import { extractIsSupportTheming } from '../common/meta/extractIsSupportTheming';
import { extractIsTrusted } from '../common/meta/extractIsTrusted';
import { extractLinkIcon } from '../flexible/icon';
import { extractInvokeDownloadAction } from './extract-invoke-download-action';
import { extractInvokeViewAction } from './extract-invoke-view-action';
export var 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 = extractSmartLinkAri(response);
  var url = extractSmartLinkUrl(response);
  var name = extractSmartLinkTitle(response);
  var hasPreviewPanelParams = Boolean(expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && isPreviewPanelAvailable && openPreviewPanel && ari && url && name);
  var hasPreviewPanel = hasPreviewPanelParams && isPreviewPanelAvailable({
    ari: ari
  });
  var isInPreviewPanel = expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && isWithinPreviewPanelIFrame();
  var data = response.data;
  var meta = response.meta;
  if (!canShowAction(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 (!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 = extractPreviewData(data, 'web')) === null || _extractPreviewData === void 0 ? void 0 : _extractPreviewData.src;

  // disallow preview modals within preview panels
  if (!hasPreviewPanel && isInPreviewPanel) {
    return;
  }
  if (src) {
    var extensionKey = getExtensionKey(response);
    return {
      hasPreviewPanel: hasPreviewPanel,
      invokeAction: {
        actionFn: function () {
          var _actionFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            var _extractSmartLinkEmbe, resolvedUrl, responseUrl, _extractSmartLinkProv, _extractSmartLinkEmbe2, _actionOptions$previe, _actionOptions$previe2;
            return _regeneratorRuntime.wrap(function (_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  if (!hasPreviewPanel) {
                    _context.next = 1;
                    break;
                  }
                  resolvedUrl = url;
                  if (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: expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') ? (_extractSmartLinkEmbe = extractSmartLinkEmbed(response)) === null || _extractSmartLinkEmbe === void 0 ? void 0 : _extractSmartLinkEmbe.src : undefined
                    }
                  });
                  expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && firePreviewActionUIEvent({
                    previewType: 'panel'
                  });
                  _context.next = 3;
                  break;
                case 1:
                  _context.next = 2;
                  return openEmbedModal(_objectSpread({
                    fireEvent: fireEvent,
                    extensionKey: extensionKey,
                    id: id,
                    invokeDownloadAction: extractInvokeDownloadAction(param),
                    invokeViewAction: extractInvokeViewAction(param, true),
                    isSupportTheming: extractIsSupportTheming(meta),
                    isTrusted: extractIsTrusted(meta),
                    linkIcon: extractLinkIcon(response),
                    providerName: (_extractSmartLinkProv = extractSmartLinkProvider(response)) === null || _extractSmartLinkProv === void 0 ? void 0 : _extractSmartLinkProv.text,
                    onClose: onClose,
                    origin: origin,
                    src: (_extractSmartLinkEmbe2 = extractSmartLinkEmbed(response)) === null || _extractSmartLinkEmbe2 === void 0 ? void 0 : _extractSmartLinkEmbe2.src,
                    title: 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
                  }, expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && {
                    isInPreviewPanel: isInPreviewPanel
                  }));
                case 2:
                  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: ActionName.PreviewAction,
        display: display,
        extensionKey: extensionKey,
        id: id
      }
    };
  }
};