import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
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 } from '../../../../common/rovo-chat-utils';
import Action from '../action';
const DEFAULT_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;
    }
    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,
        url: data === null || data === void 0 ? void 0 : data.url,
        product: data === null || data === void 0 ? void 0 : data.product,
        iconSize: props.iconSize,
        cardAppearance: 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;