/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("./index.compiled.css");
var _runtime = require("@compiled/react/runtime");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntl = require("react-intl");
var _new = _interopRequireDefault(require("@atlaskit/button/new"));
var _widthDetector = require("@atlaskit/width-detector");
var _useInvokeClientAction = _interopRequireDefault(require("../../../../state/hooks/use-invoke-client-action"));
var _useRovoChat2 = _interopRequireDefault(require("../../../../state/hooks/use-rovo-chat"));
var _rovoChatUtils = require("../../../common/rovo-chat-utils");
var _aiEditIcon = _interopRequireDefault(require("../../../FlexibleCard/assets/ai-edit-icon"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
var HIDE_SECONDARY_ACTIONS_WIDTH = 320;
var PRIMARY_ACTION_ICON_ONLY_WIDTH = 220;
var styles = {
  actionGroup: "_zulp1b66 _1e0c1txw _2lx2vrvc"
};
var RovoPromptButton = function RovoPromptButton(_ref) {
  var _ref$hideContent = _ref.hideContent,
    hideContent = _ref$hideContent === void 0 ? false : _ref$hideContent,
    icon = _ref.icon,
    onClick = _ref.onClick,
    promptAction = _ref.promptAction,
    promptKey = _ref.promptKey,
    testId = _ref.testId;
  var iconBefore = (0, _react.useCallback)(function () {
    return /*#__PURE__*/_react.default.createElement("span", {
      "aria-hidden": "true"
    }, icon);
  }, [icon]);
  var label = promptAction.data.name;
  var handleClick = (0, _react.useCallback)(function (event) {
    return onClick(event, promptAction.data, promptKey);
  }, [onClick, promptAction.data, promptKey]);
  return /*#__PURE__*/_react.default.createElement(_new.default, {
    appearance: "subtle",
    "aria-label": hideContent ? label : undefined,
    iconBefore: iconBefore,
    onClick: handleClick,
    spacing: "compact",
    testId: "".concat(testId, "-").concat(promptKey)
  }, hideContent ? null : label);
};
var EmbedRovoActionsFooter = function EmbedRovoActionsFooter(_ref2) {
  var actionData = _ref2.actionData,
    prompts = _ref2.prompts,
    _ref2$testId = _ref2.testId,
    testId = _ref2$testId === void 0 ? 'embed-rovo-actions-footer' : _ref2$testId;
  var intl = (0, _reactIntl.useIntl)();
  var invoke = (0, _useInvokeClientAction.default)({});
  var _useRovoChat = (0, _useRovoChat2.default)(),
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled,
    sendPromptMessage = _useRovoChat.sendPromptMessage;
  var _useState = (0, _react.useState)(undefined),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    containerWidth = _useState2[0],
    setContainerWidth = _useState2[1];
  var shouldHideSecondaryActions = containerWidth !== undefined && containerWidth < HIDE_SECONDARY_ACTIONS_WIDTH;
  var shouldHidePrimaryActionContent = containerWidth !== undefined && containerWidth < PRIMARY_ACTION_ICON_ONLY_WIDTH;
  var onClick = (0, _react.useCallback)(function (event, promptData, promptKey) {
    event.preventDefault();
    event.stopPropagation();
    invoke(_objectSpread(_objectSpread({}, actionData.invokeAction), {}, {
      actionFn: function () {
        var _actionFn = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
          return _regenerator.default.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
    }));
  }, [actionData.invokeAction, invoke, sendPromptMessage]);
  var promptActions = (0, _react.useMemo)(function () {
    return prompts.reduce(function (actions, promptKey) {
      if (shouldHideSecondaryActions && actions.length > 0) {
        return actions;
      }
      var promptAction = (0, _rovoChatUtils.getPromptAction)({
        promptKey: promptKey,
        intl: intl,
        url: actionData.url,
        product: actionData.product,
        iconSize: 'small'
      });
      if (!promptAction) {
        return actions;
      }
      var isPrimaryAction = actions.length === 0;
      actions.push( /*#__PURE__*/_react.default.createElement(RovoPromptButton, {
        hideContent: isPrimaryAction && shouldHidePrimaryActionContent,
        icon: isPrimaryAction ? /*#__PURE__*/_react.default.createElement(_aiEditIcon.default, null) : promptAction.icon,
        key: promptKey,
        onClick: onClick,
        promptAction: promptAction,
        promptKey: promptKey,
        testId: testId
      }));
      return actions;
    }, []);
  }, [actionData.product, actionData.url, intl, onClick, prompts, shouldHidePrimaryActionContent, shouldHideSecondaryActions, testId]);
  if (!isRovoChatEnabled || promptActions.length === 0) {
    return null;
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-testid": testId
  }, /*#__PURE__*/_react.default.createElement(_widthDetector.WidthObserver, {
    setWidth: setContainerWidth,
    offscreen: true
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _runtime.ax)([styles.actionGroup])
  }, promptActions));
};
var _default = exports.default = EmbedRovoActionsFooter;