/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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) { _defineProperty(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; }
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
import { extractSmartLinkProvider } from '@atlaskit/link-extractors';
import { Box, Text } from '@atlaskit/primitives/compiled';
import extractRovoChatAction from '../../../../extractors/flexible/actions/extract-rovo-chat-action';
import { getExtensionKey } from '../../../../state/helpers';
import useInvokeClientAction from '../../../../state/hooks/use-invoke-client-action';
import useRovoChat from '../../../../state/hooks/use-rovo-chat';
import useRovoConfig from '../../../../state/hooks/use-rovo-config';
import { useSmartCardState } from '../../../../state/store';
import { getPromptAction, RovoChatPromptKey } from '../../../common/rovo-chat-utils';
import { ActionButton } from '../action-button';
var styles = {
  text: "_19bv12x7 _1e0c116y _4cvr1h6o _s7n4nkob _19pk1i6y"
};
export var InlineRovoActionButton = function InlineRovoActionButton(_ref) {
  var testId = _ref.testId,
    url = _ref.url,
    actionOptions = _ref.actionOptions;
  var _useRovoChat = useRovoChat(),
    sendPromptMessage = _useRovoChat.sendPromptMessage,
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled;
  var intl = useIntl();
  var cardState = useSmartCardState(url !== null && url !== void 0 ? url : '');
  var extensionKey = getExtensionKey(cardState.details);
  var rovoConfig = useRovoConfig();
  var rovoChatAction = useMemo(function () {
    return cardState.details && extractRovoChatAction({
      response: cardState.details,
      actionOptions: actionOptions,
      rovoConfig: rovoConfig
    });
  }, [cardState.details, rovoConfig, actionOptions]);
  var provider = useMemo(function () {
    return cardState.details && extractSmartLinkProvider(cardState.details);
  }, [cardState.details]);
  var invoke = useInvokeClientAction({});
  var promptKey = 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 RovoChatPromptKey.SUMMARIZE_PRESENTATION;
        }
        return RovoChatPromptKey.SUMMARIZE_DOCUMENT;
      case 'onedrive-object-provider':
        return RovoChatPromptKey.SUMMARIZE_DOCUMENT;
      case 'github-object-provider':
      case 'gitlab-object-provider':
        return RovoChatPromptKey.EXPLAIN_CODE;
      case 'slack-object-provider':
      case 'ms-teams-object-provider':
        return RovoChatPromptKey.CATCH_UP;
      case 'salesforce-object-provider':
        return RovoChatPromptKey.SALESFORCE_PREP;
    }
  }, [extensionKey, cardState]);
  var _ref2 = promptKey ? 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 = 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            return _regeneratorRuntime.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.createElement(ActionButton, {
    onClick: handleClick,
    testId: testId
  }, icon, /*#__PURE__*/React.createElement(Box, {
    xcss: styles.text
  }, /*#__PURE__*/React.createElement(Text, {
    size: "small"
  }, content))) : null;
};