/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InlineRovoActionButton = void 0;
require("./index.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _linkExtractors = require("@atlaskit/link-extractors");
var _compiled = require("@atlaskit/primitives/compiled");
var _extractRovoChatAction = _interopRequireDefault(require("../../../../extractors/flexible/actions/extract-rovo-chat-action"));
var _helpers = require("../../../../state/helpers");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../state/hooks/use-invoke-client-action"));
var _useRovoChat2 = _interopRequireDefault(require("../../../../state/hooks/use-rovo-chat"));
var _useRovoConfig = _interopRequireDefault(require("../../../../state/hooks/use-rovo-config"));
var _store = require("../../../../state/store");
var _rovoChatUtils = require("../../../common/rovo-chat-utils");
var _actionButton = require("../action-button");
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 styles = {
  text: "_19bv12x7 _1e0c116y _4cvr1h6o _s7n4nkob _19pk1i6y"
};
var InlineRovoActionButton = exports.InlineRovoActionButton = function InlineRovoActionButton(_ref) {
  var testId = _ref.testId,
    url = _ref.url,
    actionOptions = _ref.actionOptions;
  var _useRovoChat = (0, _useRovoChat2.default)(),
    sendPromptMessage = _useRovoChat.sendPromptMessage,
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled;
  var intl = (0, _reactIntl.useIntl)();
  var cardState = (0, _store.useSmartCardState)(url !== null && url !== void 0 ? url : '');
  var extensionKey = (0, _helpers.getExtensionKey)(cardState.details);
  var rovoConfig = (0, _useRovoConfig.default)();
  var rovoChatAction = (0, _react.useMemo)(function () {
    return cardState.details && (0, _extractRovoChatAction.default)({
      response: cardState.details,
      actionOptions: actionOptions,
      rovoConfig: rovoConfig
    });
  }, [cardState.details, rovoConfig, actionOptions]);
  var provider = (0, _react.useMemo)(function () {
    return cardState.details && (0, _linkExtractors.extractSmartLinkProvider)(cardState.details);
  }, [cardState.details]);
  var invoke = (0, _useInvokeClientAction.default)({});
  var promptKey = (0, _react.useMemo)(function () {
    var _cardState$details, _cardState$details2;
    if (extensionKey === 'google-object-provider' && (_cardState$details = cardState.details) !== null && _cardState$details !== void 0 && (_cardState$details = _cardState$details.data) !== null && _cardState$details !== void 0 && (_cardState$details = _cardState$details['@type']) !== null && _cardState$details !== void 0 && _cardState$details.includes('schema:SpreadsheetDigitalDocument')) {
      return;
    }
    switch (extensionKey) {
      case 'google-object-provider':
        if ((_cardState$details2 = cardState.details) !== null && _cardState$details2 !== void 0 && (_cardState$details2 = _cardState$details2.data) !== null && _cardState$details2 !== void 0 && (_cardState$details2 = _cardState$details2['@type']) !== null && _cardState$details2 !== void 0 && _cardState$details2.includes('schema:PresentationDigitalDocument')) {
          return _rovoChatUtils.RovoChatPromptKey.SUMMARIZE_PRESENTATION;
        }
        return _rovoChatUtils.RovoChatPromptKey.SUMMARIZE_DOCUMENT;
      case 'onedrive-object-provider':
        return _rovoChatUtils.RovoChatPromptKey.SUMMARIZE_DOCUMENT;
      case 'github-object-provider':
      case 'gitlab-object-provider':
        return _rovoChatUtils.RovoChatPromptKey.EXPLAIN_CODE;
      case 'slack-object-provider':
      case 'ms-teams-object-provider':
        return _rovoChatUtils.RovoChatPromptKey.CATCH_UP;
      case 'salesforce-object-provider':
        return _rovoChatUtils.RovoChatPromptKey.SALESFORCE_PREP;
    }
  }, [extensionKey, cardState]);
  var _ref2 = promptKey ? (0, _rovoChatUtils.getPromptAction)({
      promptKey: promptKey,
      intl: intl,
      url: url,
      iconSize: 'small',
      provider: provider === null || provider === void 0 ? void 0 : provider.text
    }) || {} : {},
    promptData = _ref2.data,
    content = _ref2.content,
    icon = _ref2.icon;
  var handleClick = (0, _react.useCallback)(function (event) {
    event.preventDefault();
    event.stopPropagation();
    if (isRovoChatEnabled && promptData && rovoChatAction !== null && rovoChatAction !== void 0 && rovoChatAction.invokeAction) {
      invoke(_objectSpread(_objectSpread({}, rovoChatAction === null || rovoChatAction === void 0 ? void 0 : rovoChatAction.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
      }));
    }
  }, [sendPromptMessage, isRovoChatEnabled, promptData, rovoChatAction, promptKey, invoke]);
  return promptData && content ? /*#__PURE__*/_react.default.createElement(_actionButton.ActionButton, {
    onClick: handleClick,
    testId: testId
  }, icon, /*#__PURE__*/_react.default.createElement(_compiled.Box, {
    xcss: styles.text
  }, /*#__PURE__*/_react.default.createElement(_compiled.Text, {
    size: "small"
  }, content))) : null;
};