import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick"];
import React, { useCallback, useMemo } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
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';
var PreviewAction = function PreviewAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = _objectWithoutProperties(_ref, _excluded);
  var intl = useIntl();
  var context = useFlexibleUiContext();
  var invoke = useInvokeClientAction({});
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.PreviewAction];
  var hasPreviewPanel = data === null || data === void 0 ? void 0 : data.hasPreviewPanel;
  var onClick = useCallback(function () {
    if (data !== null && data !== void 0 && data.invokeAction) {
      invoke(data.invokeAction);
      onClickCallback === null || onClickCallback === void 0 || onClickCallback();
    }
  }, [data, invoke, onClickCallback]);
  var isStackItem = props.as === 'stack-item';
  var tooltipMessage = isStackItem ? messages.preview_description : messages.preview_improved;
  var actionIcon = useCallback(function () {
    // 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: "",
        size: props.iconSize
      });
    }
    return /*#__PURE__*/React.createElement(MediaServicesActualSizeIcon, {
      color: "currentColor",
      spacing: "spacious",
      label: "",
      size: props.iconSize
    });
  }, [hasPreviewPanel, props.iconSize]);
  var actionLabel = useMemo(function () {
    // Only use panel message if experiment is enabled and hasPreviewPanel is true
    if (expValEquals('platform_hover_card_preview_panel', 'cohort', 'test') && hasPreviewPanel) {
      return 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 messages.preview_modal;
    }
    return messages.preview_improved;
  }, [hasPreviewPanel]);
  var actionMessage = intl.formatMessage(actionLabel);
  return data ? /*#__PURE__*/React.createElement(Action, _extends({
    ariaLabel: actionMessage,
    content: actionMessage,
    icon: actionIcon(),
    onClick: onClick,
    testId: "smart-action-preview-action",
    tooltipMessage: /*#__PURE__*/React.createElement(FormattedMessage, tooltipMessage),
    hideTooltipOnMouseDown: true
  }, data, props)) : null;
};
export default PreviewAction;