/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
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';
const styles = {
  text: "_19bv12x7 _1e0c116y _4cvr1h6o _s7n4nkob _19pk1i6y"
};
export const InlineRovoActionButton = ({
  testId,
  url,
  actionOptions
}) => {
  const {
    sendPromptMessage,
    isRovoChatEnabled
  } = useRovoChat();
  const intl = useIntl();
  const cardState = useSmartCardState(url !== null && url !== void 0 ? url : '');
  const extensionKey = getExtensionKey(cardState.details);
  const rovoConfig = useRovoConfig();
  const rovoChatAction = useMemo(() => {
    return cardState.details && extractRovoChatAction({
      response: cardState.details,
      actionOptions,
      rovoConfig
    });
  }, [cardState.details, rovoConfig, actionOptions]);
  const provider = useMemo(() => {
    return cardState.details && extractSmartLinkProvider(cardState.details);
  }, [cardState.details]);
  const invoke = useInvokeClientAction({});
  const promptKey = useMemo(() => {
    var _cardState$details, _cardState$details$da, _cardState$details$da2, _cardState$details2, _cardState$details2$d, _cardState$details2$d2;
    if (extensionKey === 'google-object-provider' && (_cardState$details = cardState.details) !== null && _cardState$details !== void 0 && (_cardState$details$da = _cardState$details.data) !== null && _cardState$details$da !== void 0 && (_cardState$details$da2 = _cardState$details$da['@type']) !== null && _cardState$details$da2 !== void 0 && _cardState$details$da2.includes('schema:SpreadsheetDigitalDocument')) {
      return;
    }
    switch (extensionKey) {
      case 'google-object-provider':
        if ((_cardState$details2 = cardState.details) !== null && _cardState$details2 !== void 0 && (_cardState$details2$d = _cardState$details2.data) !== null && _cardState$details2$d !== void 0 && (_cardState$details2$d2 = _cardState$details2$d['@type']) !== null && _cardState$details2$d2 !== void 0 && _cardState$details2$d2.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]);
  const {
    data: promptData,
    content,
    icon
  } = promptKey ? getPromptAction({
    promptKey,
    intl,
    url,
    iconSize: 'small',
    provider: provider === null || provider === void 0 ? void 0 : provider.text
  }) || {} : {};
  const handleClick = useCallback(event => {
    event.preventDefault();
    event.stopPropagation();
    if (isRovoChatEnabled && promptData && rovoChatAction !== null && rovoChatAction !== void 0 && rovoChatAction.invokeAction) {
      invoke({
        ...(rovoChatAction === null || rovoChatAction === void 0 ? void 0 : rovoChatAction.invokeAction),
        actionFn: async () => sendPromptMessage(promptData),
        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;
};