import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
import AiSearchIcon from '@atlaskit/icon-lab/core/ai-search';
import AiChatIcon from '@atlaskit/icon/core/ai-chat';
import AiGenerativeTextSummaryIcon from '@atlaskit/icon/core/ai-generative-text-summary';
import RovoChatIcon from '@atlaskit/icon/core/rovo-chat';
import { RovoIcon } from '@atlaskit/logo';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { ActionName, CardDisplay } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext } from '../../../../../state/flexible-ui-context';
import { isBlockCardRovoActionExperimentEnabled } from '../../../../../state/hooks/use-block-card-rovo-action-experiment';
import useInvokeClientAction from '../../../../../state/hooks/use-invoke-client-action';
import useRovoChat from '../../../../../state/hooks/use-rovo-chat';
import AiChapterIcon from '../../../assets/ai-chapter-icon';
import AIEditIcon from '../../../assets/ai-edit-icon';
import AISearchIcon from '../../../assets/ai-search-icon';
import Action from '../action';
import htmlToAdf from './html-to-adf';
export let RovoChatPromptKey = /*#__PURE__*/function (RovoChatPromptKey) {
  RovoChatPromptKey["RECOMMEND_OTHER_SOURCES"] = "recommend-other-sources";
  RovoChatPromptKey["SHOW_OTHER_MENTIONS"] = "show-other-mentions";
  RovoChatPromptKey["SUGGEST_IMPROVEMENT"] = "suggest-improvement";
  RovoChatPromptKey["SUMMARIZE_LINK"] = "summarize-link";
  RovoChatPromptKey["KEY_HIGHLIGHTS"] = "key-highlights";
  RovoChatPromptKey["ASK_ROVO_ANYTHING"] = "ask-rovo-anything";
  RovoChatPromptKey["IDENTIFY_KEY_POINTS"] = "identify-key-points";
  RovoChatPromptKey["IDENTIFY_KEY_TRENDS"] = "identify-key-trends";
  RovoChatPromptKey["FIND_OPEN_QUESTIONS"] = "find-open-questions";
  RovoChatPromptKey["HIGHLIGHT_RELEVANT_CONTENT"] = "highlight-relevant-content";
  RovoChatPromptKey["SUMMARIZE_THIS_FOR_ME"] = "summarize-this-for-me";
  RovoChatPromptKey["ASK_A_SPECIFIC_QUESTION"] = "ask-a-specific-question";
  RovoChatPromptKey["SHOW_ME_WHATS_RELEVANT"] = "show-me-whats-relevant";
  return RovoChatPromptKey;
}({});
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 getContext = (intl, product) => {
  switch (product) {
    case 'CONFLUENCE':
      return {
        contextLong: intl.formatMessage(messages.rovo_prompt_context_confluence_page),
        contextShort: intl.formatMessage(messages.rovo_prompt_context_confluence_page_short)
      };
    case 'JSW':
    case 'JWM':
    case 'JSM':
    case 'JPD':
      return {
        contextLong: intl.formatMessage(messages.rovo_prompt_context_jira_work_item),
        contextShort: intl.formatMessage(messages.rovo_prompt_context_jira_work_item_short)
      };
  }
};
const getPromptAction = (promptKey, intl, url = '', product, iconSize, cardAppearance) => {
  var _getContext;
  const {
    contextLong,
    contextShort
  } = (_getContext = getContext(intl, product)) !== null && _getContext !== void 0 ? _getContext : {
    contextLong: intl.formatMessage(messages.rovo_prompt_context_generic),
    contextShort: intl.formatMessage(messages.rovo_prompt_context_generic)
  };
  const isBlockCard3PExperimentEnabled = isBlockCardRovoActionExperimentEnabled(product);
  switch (promptKey) {
    case RovoChatPromptKey.RECOMMEND_OTHER_SOURCES:
      const label_recommend = intl.formatMessage(messages.rovo_prompt_button_recommend_other_sources);
      const html_recommend = intl.formatMessage(messages.rovo_prompt_message_recommend_other_sources, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AIEditIcon, null),
        content: label_recommend,
        tooltipMessage: label_recommend,
        data: {
          name: label_recommend,
          dialogues: [],
          prompt: htmlToAdf(html_recommend)
        }
      };
    case RovoChatPromptKey.SHOW_OTHER_MENTIONS:
      const label_other_mentions = intl.formatMessage(messages.rovo_prompt_button_show_other_mentions);
      const html_other_mentions = intl.formatMessage(messages.rovo_prompt_message_show_other_mentions, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChapterIcon, null),
        content: label_other_mentions,
        tooltipMessage: label_other_mentions,
        data: {
          name: label_other_mentions,
          dialogues: [],
          prompt: htmlToAdf(html_other_mentions)
        }
      };
    case RovoChatPromptKey.SUGGEST_IMPROVEMENT:
      const label_improvement = intl.formatMessage(messages.rovo_prompt_button_suggest_improvement, {
        context: contextShort
      });
      const html_improvement = intl.formatMessage(messages.rovo_prompt_message_suggest_improvement, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AISearchIcon, null),
        content: label_improvement,
        tooltipMessage: label_improvement,
        data: {
          name: label_improvement,
          dialogues: [],
          prompt: htmlToAdf(html_improvement)
        }
      };
    case RovoChatPromptKey.SUMMARIZE_LINK:
      const label_summarize = intl.formatMessage(messages.ai_summarize);
      const html_summarize = intl.formatMessage(messages.rovo_prompt_message_summarize, {
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: cardAppearance === CardDisplay.Block && isBlockCard3PExperimentEnabled ? /*#__PURE__*/React.createElement(AiChatIcon, {
          label: label_summarize,
          size: iconSize
        }) : /*#__PURE__*/React.createElement(AIEditIcon, null),
        content: label_summarize,
        tooltipMessage: label_summarize,
        data: {
          name: label_summarize,
          dialogues: [],
          prompt: htmlToAdf(html_summarize),
          mode: {
            fastModeEnabled: true
          }
        }
      };
    case RovoChatPromptKey.SUMMARIZE_THIS_FOR_ME:
      const label_summarize_this_for_me = intl.formatMessage(messages.rovo_prompt_button_summarize_this);
      const html_summarize_this_for_me = intl.formatMessage(messages.rovo_prompt_message_summarize, {
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiGenerativeTextSummaryIcon, {
          label: label_summarize_this_for_me
        }),
        content: label_summarize_this_for_me,
        tooltipMessage: label_summarize_this_for_me,
        data: {
          name: label_summarize_this_for_me,
          dialogues: [],
          prompt: htmlToAdf(html_summarize_this_for_me),
          mode: {
            fastModeEnabled: true
          }
        }
      };
    case RovoChatPromptKey.HIGHLIGHT_RELEVANT_CONTENT:
      const label_highlight_relevant_content = intl.formatMessage(messages.rovo_prompt_button_highlight_relevant_content);
      const html_highlight_relevant_content = intl.formatMessage(messages.rovo_prompt_message_highlight_relevant_content, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChapterIcon, null),
        content: label_highlight_relevant_content,
        tooltipMessage: label_highlight_relevant_content,
        data: {
          name: label_highlight_relevant_content,
          dialogues: [],
          prompt: htmlToAdf(html_highlight_relevant_content),
          mode: {
            fastModeEnabled: true
          }
        }
      };
    case RovoChatPromptKey.ASK_ROVO_ANYTHING:
      const label_ask_rovo_anything = intl.formatMessage(messages.rovo_prompt_button_ask_rovo_anything);
      const prompt_ask_rovo_anything = intl.formatMessage(messages.rovo_prompt_message_ask_rovo_anything, {
        url
      });
      return {
        icon: cardAppearance === CardDisplay.Block && isBlockCard3PExperimentEnabled ? /*#__PURE__*/React.createElement(RovoIcon, {
          label: label_ask_rovo_anything,
          size: 'xxsmall',
          shouldUseHexLogo: true
        }) : /*#__PURE__*/React.createElement(AISearchIcon, null),
        content: label_ask_rovo_anything,
        tooltipMessage: label_ask_rovo_anything,
        data: {
          name: label_ask_rovo_anything,
          dialogues: [],
          prompt: prompt_ask_rovo_anything,
          isPromptPlaceholder: true,
          placeholderType: 'generic'
        }
      };
    case RovoChatPromptKey.ASK_A_SPECIFIC_QUESTION:
      const label_ask_a_specific_question = intl.formatMessage(messages.rovo_prompt_button_ask_a_specific_question);
      const prompt_ask_a_specific_question = intl.formatMessage(messages.rovo_prompt_message_ask_rovo_anything, {
        url
      });
      return {
        icon: /*#__PURE__*/React.createElement(RovoChatIcon, {
          label: label_ask_a_specific_question
        }),
        content: label_ask_a_specific_question,
        tooltipMessage: label_ask_a_specific_question,
        data: {
          name: label_ask_a_specific_question,
          dialogues: [],
          prompt: prompt_ask_a_specific_question,
          isPromptPlaceholder: true,
          placeholderType: 'generic'
        }
      };
    case RovoChatPromptKey.KEY_HIGHLIGHTS:
      const label_key_highlights = intl.formatMessage(messages.rovo_prompt_button_key_highlights);
      const html_key_highlights = intl.formatMessage(messages.rovo_prompt_message_key_highlights, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChatIcon, {
          label: label_key_highlights,
          size: iconSize
        }),
        content: label_key_highlights,
        tooltipMessage: label_key_highlights,
        data: {
          name: label_key_highlights,
          dialogues: [],
          prompt: htmlToAdf(html_key_highlights)
        }
      };
    case RovoChatPromptKey.SHOW_ME_WHATS_RELEVANT:
      const label_show_me_whats_relevant = intl.formatMessage(messages.rovo_prompt_button_show_me_whats_relevant);
      const html_show_me_whats_relevant = intl.formatMessage(messages.rovo_prompt_message_highlight_relevant_content, {
        context: contextLong,
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiSearchIcon, {
          label: label_show_me_whats_relevant
        }),
        content: label_show_me_whats_relevant,
        tooltipMessage: label_show_me_whats_relevant,
        data: {
          name: label_show_me_whats_relevant,
          dialogues: [],
          prompt: htmlToAdf(html_show_me_whats_relevant)
        }
      };
    case RovoChatPromptKey.IDENTIFY_KEY_TRENDS:
      const label_identify_key_trends = intl.formatMessage(messages.rovo_prompt_button_identify_key_trends);
      const html_identify_key_trends = intl.formatMessage(messages.rovo_prompt_message_identify_key_trends, {
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChatIcon, {
          label: label_identify_key_trends,
          size: iconSize
        }),
        content: label_identify_key_trends,
        tooltipMessage: label_identify_key_trends,
        data: {
          name: label_identify_key_trends,
          dialogues: [],
          prompt: htmlToAdf(html_identify_key_trends)
        }
      };
    case RovoChatPromptKey.IDENTIFY_KEY_POINTS:
      const label_identify_key_points = intl.formatMessage(messages.rovo_prompt_button_identify_key_points);
      const html_identify_key_points = intl.formatMessage(messages.rovo_prompt_message_identify_key_points, {
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChatIcon, {
          label: label_identify_key_points,
          size: iconSize
        }),
        content: label_identify_key_points,
        tooltipMessage: label_identify_key_points,
        data: {
          name: label_identify_key_points,
          dialogues: [],
          prompt: htmlToAdf(html_identify_key_points)
        }
      };
    case RovoChatPromptKey.FIND_OPEN_QUESTIONS:
      const label_find_open_questions = intl.formatMessage(messages.rovo_prompt_button_find_open_questions);
      const html_find_open_questions = intl.formatMessage(messages.rovo_prompt_message_find_open_questions, {
        url
      }, {
        ignoreTag: true
      });
      return {
        icon: /*#__PURE__*/React.createElement(AiChatIcon, {
          label: label_find_open_questions,
          size: iconSize
        }),
        content: label_find_open_questions,
        tooltipMessage: label_find_open_questions,
        data: {
          name: label_find_open_questions,
          dialogues: [],
          prompt: htmlToAdf(html_find_open_questions)
        }
      };
  }
};
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;