"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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _constants = require("../../../../../constants");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../../state/hooks/use-invoke-client-action"));
var _useRovoChat2 = _interopRequireDefault(require("../../../../../state/hooks/use-rovo-chat"));
var _rovoChatUtils = require("../../../../common/rovo-chat-utils");
var _action = _interopRequireDefault(require("../action"));
var _excluded = ["onClick", "prompts", "testId"];
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var GOOGLE_PROMPTS = [_rovoChatUtils.RovoChatPromptKey.RECOMMEND_OTHER_SOURCES, _rovoChatUtils.RovoChatPromptKey.SHOW_OTHER_MENTIONS, _rovoChatUtils.RovoChatPromptKey.SUGGEST_IMPROVEMENT];
var GENERIC_3P_PROMPTS = [
// For rovogrowth-640-inline-action-nudge-exp only
_rovoChatUtils.RovoChatPromptKey.SHOW_ME_WHATS_RELEVANT, _rovoChatUtils.RovoChatPromptKey.SUMMARIZE_THIS_FOR_ME, _rovoChatUtils.RovoChatPromptKey.ASK_A_SPECIFIC_QUESTION];
var DEFAULT_PROMPTS = GOOGLE_PROMPTS;
var RovoChatAction = function RovoChatAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    prompts = _ref.prompts,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-action-rovo-chat-action' : _ref$testId,
    props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var intl = (0, _reactIntl.useIntl)();
  var _useRovoChat = (0, _useRovoChat2.default)(),
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled,
    sendPromptMessage = _useRovoChat.sendPromptMessage;
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.RovoChatAction];
  var resolvedPrompts = (0, _react.useMemo)(function () {
    if (prompts) {
      return prompts;
    }
    if ((0, _expValEqualsNoExposure.expValEqualsNoExposure)('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true)) {
      return GENERIC_3P_PROMPTS;
    }
    return DEFAULT_PROMPTS;
  }, [prompts]);
  var invoke = (0, _useInvokeClientAction.default)({});
  var _onClick = (0, _react.useCallback)(function (promptData, promptKey) {
    if (promptData && data !== null && data !== void 0 && data.invokeAction) {
      invoke(_objectSpread(_objectSpread({}, data === null || data === void 0 ? void 0 : data.invokeAction), {}, {
        actionFn: function () {
          var _actionFn = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
            return _regenerator.default.wrap(function (_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  return _context.abrupt("return", sendPromptMessage(promptData));
                case 1:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          }));
          function actionFn() {
            return _actionFn.apply(this, arguments);
          }
          return actionFn;
        }(),
        prompt: promptKey
      }));
      onClickCallback === null || onClickCallback === void 0 || onClickCallback();
    }
  }, [data === null || data === void 0 ? void 0 : data.invokeAction, invoke, onClickCallback, sendPromptMessage]);
  var promptActions = (0, _react.useMemo)(function () {
    return resolvedPrompts.map(function (promptKey, idx) {
      var _ref2 = (0, _rovoChatUtils.getPromptAction)(promptKey, intl, data === null || data === void 0 ? void 0 : data.url, data === null || data === void 0 ? void 0 : data.product, props.iconSize, props.cardAppearance) || {},
        icon = _ref2.icon,
        content = _ref2.content,
        tooltipMessage = _ref2.tooltipMessage,
        promptData = _ref2.data;
      return promptData ? /*#__PURE__*/_react.default.createElement(_action.default, (0, _extends2.default)({
        content: content,
        icon: icon,
        key: promptKey,
        onClick: function onClick() {
          return _onClick(promptData, promptKey);
        },
        testId: "".concat(testId, "-").concat(idx + 1),
        tooltipMessage: tooltipMessage
      }, props)) : null;
    });
  }, [data, intl, _onClick, resolvedPrompts, props, testId]);
  return isRovoChatEnabled && data && (promptActions === null || promptActions === void 0 ? void 0 : promptActions.length) > 0 ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, promptActions) : null;
};
var _default = exports.default = RovoChatAction;