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';
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';
var DEFAULT_PROMPTS = [];
var RovoChatAction = function RovoChatAction(_ref) {
  var _context$actions;
  var onClickCallback = _ref.onClick,
    prompts = _ref.prompts,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-action-rovo-chat-action' : _ref$testId,
    props = _objectWithoutProperties(_ref, _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[ActionName.RovoChatAction];
  var resolvedPrompts = useMemo(function () {
    if (prompts) {
      return prompts;
    }
    return DEFAULT_PROMPTS;
  }, [prompts]);
  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 (_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 resolvedPrompts.map(function (promptKey, idx) {
      var _ref2 = getPromptAction({
          promptKey: promptKey,
          intl: 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
        }) || {},
        icon = _ref2.icon,
        content = _ref2.content,
        tooltipMessage = _ref2.tooltipMessage,
        promptData = _ref2.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, 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;