import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import MediaServicesActualSizeIcon from '@atlaskit/icon/core/grow-diagonal';
import PanelRightIcon from '@atlaskit/icon/core/panel-right';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { ActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import { useBlockCardRovoActionExperimentNoExposure } from '../../../../../state/hooks/use-block-card-rovo-action-experiment';
import useInvokeClientAction from '../../../../../state/hooks/use-invoke-client-action';
import Action from '../action';
const PreviewAction = ({
  onClick: onClickCallback,
  ...props
}) => {
  var _context$actions;
  const context = useFlexibleUiContext();
  const invoke = useInvokeClientAction({});
  const data = context === null || context === void 0 ? void 0 : (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.PreviewAction];
  const hasPreviewPanel = data === null || data === void 0 ? void 0 : data.hasPreviewPanel;
  const isRovoBlockCardExperimentEnabled = useBlockCardRovoActionExperimentNoExposure();
  const onClick = useCallback(() => {
    if (data !== null && data !== void 0 && data.invokeAction) {
      invoke(data.invokeAction);
      onClickCallback === null || onClickCallback === void 0 ? void 0 : onClickCallback();
    }
  }, [data, invoke, onClickCallback]);
  const isStackItem = props.as === 'stack-item';
  const tooltipMessage = isStackItem ? messages.preview_description : messages.preview_improved;
  const actionIcon = useCallback(() => {
    // Only use panel icon if experiment is enabled and hasPreviewPanel is true
    if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && hasPreviewPanel) {
      return /*#__PURE__*/React.createElement(PanelRightIcon, _extends({
        color: "currentColor",
        spacing: "spacious",
        label: fg('navx-3698-flexible-card-a11y-fix') ? '' : 'Open preview panel'
      }, fg('platform_sl_3p_auth_rovo_action_kill_switch') || isRovoBlockCardExperimentEnabled || expValEqualsNoExposure('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true) ? {
        size: props.iconSize
      } : {}));
    }
    return /*#__PURE__*/React.createElement(MediaServicesActualSizeIcon, _extends({
      color: "currentColor",
      spacing: "spacious",
      label: fg('navx-3698-flexible-card-a11y-fix') ? '' : 'Open preview'
    }, fg('platform_sl_3p_auth_rovo_action_kill_switch') || isRovoBlockCardExperimentEnabled || expValEqualsNoExposure('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true) ? {
      size: props.iconSize
    } : {}));
  }, [hasPreviewPanel, props.iconSize, isRovoBlockCardExperimentEnabled]);
  const actionLabel = useCallback(() => {
    // Only use panel message if experiment is enabled and hasPreviewPanel is true
    if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && hasPreviewPanel) {
      return /*#__PURE__*/React.createElement(FormattedMessage, messages.preview_panel);
    }
    // Fall back to modal message if experiment is enabled, otherwise use original preview message
    if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test')) {
      return /*#__PURE__*/React.createElement(FormattedMessage, messages.preview_modal);
    }
    return /*#__PURE__*/React.createElement(FormattedMessage, messages.preview_improved);
  }, [hasPreviewPanel]);
  return data ? /*#__PURE__*/React.createElement(Action, _extends({
    content: actionLabel(),
    icon: actionIcon(),
    onClick: onClick,
    testId: "smart-action-preview-action",
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, tooltipMessage),
    hideTooltipOnMouseDown: true
  }, data, props)) : null;
};
export default PreviewAction;