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 const extractInvokePreviewAction = param => {
  var _extractPreviewData;
  const {
    actionOptions,
    appearance: display,
    fireEvent,
    onClose,
    id,
    origin,
    response,
    isPreviewPanelAvailable,
    openPreviewPanel
  } = param;
  const ari = extractSmartLinkAri(response);
  const url = extractSmartLinkUrl(response);
  const name = extractSmartLinkTitle(response);
  const hasPreviewPanelParams = Boolean(expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && isPreviewPanelAvailable && openPreviewPanel && ari && url && name);
  const hasPreviewPanel = hasPreviewPanelParams && isPreviewPanelAvailable({
    ari: ari
  });
  const isInPreviewPanel = expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && isWithinPreviewPanelIFrame();
  const data = response.data;
  const 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 */
  const firePreviewActionUIEvent = ({
    previewType
  }) => {
    if (!expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') || origin !== 'smartLinkPreviewHoverCard' || !fireEvent) {
      return;
    }
    fireEvent('ui.smartLink.clicked.previewHoverCard', {
      id: id || '',
      display: 'hoverCardPreview',
      previewType
    });
  };
  const 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) {
    const extensionKey = getExtensionKey(response);
    return {
      hasPreviewPanel,
      invokeAction: {
        actionFn: async () => {
          if (hasPreviewPanel) {
            var _extractSmartLinkEmbe;
            let resolvedUrl = url;
            if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test')) {
              const 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'
            });
          } else {
            var _extractSmartLinkProv, _extractSmartLinkEmbe2, _actionOptions$previe, _actionOptions$previe2;
            await openEmbedModal({
              fireEvent,
              extensionKey,
              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,
              origin,
              src: (_extractSmartLinkEmbe2 = extractSmartLinkEmbed(response)) === null || _extractSmartLinkEmbe2 === void 0 ? void 0 : _extractSmartLinkEmbe2.src,
              title: extractSmartLinkTitle(response),
              url,
              size: actionOptions === null || actionOptions === void 0 ? void 0 : (_actionOptions$previe = actionOptions.previewAction) === null || _actionOptions$previe === void 0 ? void 0 : _actionOptions$previe.size,
              isBlanketHidden: actionOptions === null || actionOptions === void 0 ? 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
              })
            });
            expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && firePreviewActionUIEvent({
              previewType: 'modal'
            });
          }
        },
        actionSubjectId: 'invokePreviewScreen',
        actionType: ActionName.PreviewAction,
        display,
        extensionKey,
        id
      }
    };
  }
};