import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onClick", "prompts", "testId"];
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-next';
import { InternalActionName } from '../../../../../constants';
import { messages } from '../../../../../messages';
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 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 var RovoChatPromptKey = /*#__PURE__*/function (RovoChatPromptKey) {
  RovoChatPromptKey["RECOMMEND_OTHER_SOURCES"] = "recommend-other-sources";
  RovoChatPromptKey["SHOW_OTHER_MENTIONS"] = "show-other-mentions";
  RovoChatPromptKey["SUGGEST_IMPROVEMENT"] = "suggest-improvement";
  return RovoChatPromptKey;
}({});
var DEFAULT_PROMPTS = [RovoChatPromptKey.RECOMMEND_OTHER_SOURCES, RovoChatPromptKey.SHOW_OTHER_MENTIONS, RovoChatPromptKey.SUGGEST_IMPROVEMENT];
var getContext = function 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)
      };
  }
};
var getPromptAction = function getPromptAction(promptKey, intl) {
  var _getContext;
  var url = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
  var product = arguments.length > 3 ? arguments[3] : undefined;
  var _ref = (_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)
    },
    contextLong = _ref.contextLong,
    contextShort = _ref.contextShort;
  switch (promptKey) {
    case RovoChatPromptKey.RECOMMEND_OTHER_SOURCES:
      var label_recommend = intl.formatMessage(messages.rovo_prompt_button_recommend_other_sources);
      var html_recommend = intl.formatMessage(messages.rovo_prompt_message_recommend_other_sources, {
        context: contextLong,
        url: 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:
      var label_other_mentions = intl.formatMessage(messages.rovo_prompt_button_show_other_mentions);
      var html_other_mentions = intl.formatMessage(messages.rovo_prompt_message_show_other_mentions, {
        context: contextLong,
        url: 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:
      var label_improvement = intl.formatMessage(messages.rovo_prompt_button_suggest_improvement, {
        context: contextShort
      });
      var html_improvement = intl.formatMessage(messages.rovo_prompt_message_suggest_improvement, {
        context: contextLong,
        url: 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)
        }
      };
  }
};
var RovoChatAction = function RovoChatAction(_ref2) {
  var _context$actions;
  var onClickCallback = _ref2.onClick,
    _ref2$prompts = _ref2.prompts,
    prompts = _ref2$prompts === void 0 ? DEFAULT_PROMPTS : _ref2$prompts,
    _ref2$testId = _ref2.testId,
    testId = _ref2$testId === void 0 ? 'smart-action-rovo-chat-action' : _ref2$testId,
    props = _objectWithoutProperties(_ref2, _excluded);
  var intl = useIntl();
  var _useRovoChat = useRovoChat(),
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled,
    sendPromptMessage = _useRovoChat.sendPromptMessage;
  var context = useFlexibleUiContext();
  var data = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[InternalActionName.RovoChatAction];
  var invoke = useInvokeClientAction({});
  var _onClick = useCallback(function (promptData, promptKey) {
    if (promptData && data !== null && data !== void 0 && data.invokeAction) {
      invoke(_objectSpread(_objectSpread({}, data === null || data === void 0 ? void 0 : data.invokeAction), {}, {
        actionFn: function () {
          var _actionFn = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
            return _regeneratorRuntime.wrap(function _callee$(_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
      }));
      onClickCallback === null || onClickCallback === void 0 || onClickCallback();
    }
  }, [data === null || data === void 0 ? void 0 : data.invokeAction, invoke, onClickCallback, sendPromptMessage]);
  var promptActions = useMemo(function () {
    return prompts.map(function (promptKey, idx) {
      var _ref3 = getPromptAction(promptKey, intl, data === null || data === void 0 ? void 0 : data.url, data === null || data === void 0 ? void 0 : data.product) || {},
        icon = _ref3.icon,
        content = _ref3.content,
        tooltipMessage = _ref3.tooltipMessage,
        promptData = _ref3.data;
      return promptData ? /*#__PURE__*/React.createElement(Action, _extends({
        content: content,
        icon: icon,
        key: promptKey,
        onClick: function onClick() {
          return _onClick(promptData, promptKey);
        },
        testId: "".concat(testId, "-").concat(idx + 1),
        tooltipMessage: tooltipMessage
      }, props)) : null;
    });
  }, [data, intl, _onClick, prompts, 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;