"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _growDiagonal = _interopRequireDefault(require("@atlaskit/icon/core/grow-diagonal"));
var _panelRight = _interopRequireDefault(require("@atlaskit/icon/core/panel-right"));
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../../state/hooks/use-invoke-client-action"));
var _action = _interopRequireDefault(require("../action"));
var _excluded = ["onClick"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var PreviewAction = function PreviewAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var intl = (0, _reactIntl.useIntl)();
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var invoke = (0, _useInvokeClientAction.default)({});
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.PreviewAction];
  var hasPreviewPanel = data === null || data === void 0 ? void 0 : data.hasPreviewPanel;
  var onClick = (0, _react.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.messages.preview_description : _messages.messages.preview_improved;
  var actionIcon = (0, _react.useCallback)(function () {
    // Only use panel icon if experiment is enabled and hasPreviewPanel is true
    if ((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && hasPreviewPanel) {
      return /*#__PURE__*/_react.default.createElement(_panelRight.default, {
        color: "currentColor",
        spacing: "spacious",
        label: "",
        size: props.iconSize
      });
    }
    return /*#__PURE__*/_react.default.createElement(_growDiagonal.default, {
      color: "currentColor",
      spacing: "spacious",
      label: "",
      size: props.iconSize
    });
  }, [hasPreviewPanel, props.iconSize]);
  var actionLabel = (0, _react.useMemo)(function () {
    // Only use panel message if experiment is enabled and hasPreviewPanel is true
    if ((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test') && hasPreviewPanel) {
      return _messages.messages.preview_panel;
    }
    // Fall back to modal message if experiment is enabled, otherwise use original preview message
    if ((0, _expValEquals.expValEquals)('platform_hover_card_preview_panel', 'cohort', 'test')) {
      return _messages.messages.preview_modal;
    }
    return _messages.messages.preview_improved;
  }, [hasPreviewPanel]);
  var actionMessage = intl.formatMessage(actionLabel);
  return data ? /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    ariaLabel: actionMessage,
    content: actionMessage,
    icon: actionIcon(),
    onClick: onClick,
    testId: "smart-action-preview-action",
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, tooltipMessage),
    hideTooltipOnMouseDown: true
  }, data, props)) : null;
};
var _default = exports.default = PreviewAction;