"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.AISummariseAction = AISummariseAction;
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 _atlassianIntelligence = _interopRequireDefault(require("@atlaskit/icon/core/atlassian-intelligence"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _useAnalyticsEvents2 = require("../../../../../common/analytics/generated/use-analytics-events");
var _messages = require("../../../../../messages");
var _action = _interopRequireDefault(require("../action"));
var _utils = require("./utils");
var _excluded = ["onClick", "onError", "testId", "status", "summariseUrl"];
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); }
function AISummariseAction(_ref) {
  var onClickCallback = _ref.onClick,
    onErrorCallback = _ref.onError,
    testId = _ref.testId,
    status = _ref.status,
    summariseUrl = _ref.summariseUrl,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useAnalyticsEvents = (0, _useAnalyticsEvents2.useAnalyticsEvents)(),
    fireEvent = _useAnalyticsEvents.fireEvent;
  var onCompleted = (0, _react.useCallback)(function (state) {
    if ((state === null || state === void 0 ? void 0 : state.status) === 'error') {
      onErrorCallback === null || onErrorCallback === void 0 || onErrorCallback((0, _utils.getErrorMessage)(state.error));
    }
  }, [onErrorCallback]);
  var handleActionClick = (0, _react.useCallback)(function () {
    var _summariseUrl;
    fireEvent('ui.button.clicked.aiSummary', {});
    fireEvent('track.aiInteraction.initiated', {
      aiFeatureName: 'Smart Links Summary',
      proactiveAIGenerated: 0,
      userGeneratedAI: 1
    });
    (_summariseUrl = summariseUrl()) === null || _summariseUrl === void 0 || _summariseUrl.then(onCompleted, onCompleted);
    onClickCallback === null || onClickCallback === void 0 || onClickCallback();
  }, [fireEvent, onClickCallback, onCompleted, summariseUrl]);
  return /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
    content: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.ai_summary_action),
    icon: /*#__PURE__*/_react.default.createElement(_atlassianIntelligence.default, (0, _extends2.default)({
      spacing: "spacious",
      color: "currentColor",
      label: "Summarise with AI"
    }, (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_action_kill_switch') || (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_block_card_kill_switch') || (0, _expValEqualsNoExposure.expValEqualsNoExposure)('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true) ? {
      size: props.iconSize
    } : {})),
    onClick: handleActionClick,
    testId: "".concat(testId, "-summarise-action"),
    isLoading: status === 'loading',
    tooltipMessage: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _messages.messages.ai_summary_action_description)
  }, props));
}