import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback } from 'react';
import { FormattedMessage } from 'react-intl-next';
import MediaServicesActualSizeIcon from '@atlaskit/icon/core/grow-diagonal';
import PanelRightIcon from '@atlaskit/icon/core/panel-right';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { ActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
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 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, {
        color: "currentColor",
        spacing: "spacious",
        label: "Open preview panel"
      });
    }
    return /*#__PURE__*/React.createElement(MediaServicesActualSizeIcon, {
      color: "currentColor",
      spacing: "spacious",
      label: "Open preview"
    });
  }, [hasPreviewPanel]);
  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;