import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { ActionName } from '../../../../../constants';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import useInvokeClientAction from '../../../../../state/hooks/use-invoke-client-action';
import useRovoChat from '../../../../../state/hooks/use-rovo-chat';
import { getPromptAction, RovoChatPromptKey } from "../../../../common/rovo-chat-utils";
import Action from '../action';
const GOOGLE_PROMPTS = [RovoChatPromptKey.RECOMMEND_OTHER_SOURCES, RovoChatPromptKey.SHOW_OTHER_MENTIONS, RovoChatPromptKey.SUGGEST_IMPROVEMENT];
const GENERIC_3P_PROMPTS = [
// For rovogrowth-640-inline-action-nudge-exp only
RovoChatPromptKey.SHOW_ME_WHATS_RELEVANT, RovoChatPromptKey.SUMMARIZE_THIS_FOR_ME, RovoChatPromptKey.ASK_A_SPECIFIC_QUESTION];
const DEFAULT_PROMPTS = GOOGLE_PROMPTS;
const RovoChatAction = ({
  onClick: onClickCallback,
  prompts,
  testId = 'smart-action-rovo-chat-action',
  ...props
}) => {
  var _context$actions;
  const intl = useIntl();
  const {
    isRovoChatEnabled,
    sendPromptMessage
  } = useRovoChat();
  const context = useFlexibleUiContext();
  const data = context === null || context === void 0 ? void 0 : (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.RovoChatAction];
  const resolvedPrompts = useMemo(() => {
    if (prompts) {
      return prompts;
    }
    if (expValEqualsNoExposure('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true)) {
      return GENERIC_3P_PROMPTS;
    }
    return DEFAULT_PROMPTS;
  }, [prompts]);
  const invoke = useInvokeClientAction({});
  const onClick = useCallback((promptData, promptKey) => {
    if (promptData && data !== null && data !== void 0 && data.invokeAction) {
      invoke({
        ...(data === null || data === void 0 ? void 0 : data.invokeAction),
        actionFn: async () => sendPromptMessage(promptData),
        prompt: promptKey
      });
      onClickCallback === null || onClickCallback === void 0 ? void 0 : onClickCallback();
    }
  }, [data === null || data === void 0 ? void 0 : data.invokeAction, invoke, onClickCallback, sendPromptMessage]);
  const promptActions = useMemo(() => {
    return resolvedPrompts.map((promptKey, idx) => {
      const {
        icon,
        content,
        tooltipMessage,
        data: promptData
      } = 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) || {};
      return promptData ? /*#__PURE__*/React.createElement(Action, _extends({
        content: content,
        icon: icon,
        key: promptKey,
        onClick: () => onClick(promptData, promptKey),
        testId: `${testId}-${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.createElement(React.Fragment, null, promptActions) : null;
};
export default RovoChatAction;