/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
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, useState } from 'react';
import { useIntl } from 'react-intl';
import Button from '@atlaskit/button/new';
import { WidthObserver } from '@atlaskit/width-detector';
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 AIEditIcon from '../../../FlexibleCard/assets/ai-edit-icon';
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 = useCallback(function () {
    return /*#__PURE__*/React.createElement("span", {
      "aria-hidden": "true"
    }, icon);
  }, [icon]);
  var label = promptAction.data.name;
  var handleClick = useCallback(function (event) {
    return onClick(event, promptAction.data, promptKey);
  }, [onClick, promptAction.data, promptKey]);
  return /*#__PURE__*/React.createElement(Button, {
    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 = useIntl();
  var invoke = useInvokeClientAction({});
  var _useRovoChat = useRovoChat(),
    isRovoChatEnabled = _useRovoChat.isRovoChatEnabled,
    sendPromptMessage = _useRovoChat.sendPromptMessage;
  var _useState = useState(undefined),
    _useState2 = _slicedToArray(_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 = useCallback(function (event, promptData, promptKey) {
    event.preventDefault();
    event.stopPropagation();
    invoke(_objectSpread(_objectSpread({}, actionData.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
    }));
  }, [actionData.invokeAction, invoke, sendPromptMessage]);
  var promptActions = useMemo(function () {
    return prompts.reduce(function (actions, promptKey) {
      if (shouldHideSecondaryActions && actions.length > 0) {
        return actions;
      }
      var promptAction = 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.createElement(RovoPromptButton, {
        hideContent: isPrimaryAction && shouldHidePrimaryActionContent,
        icon: isPrimaryAction ? /*#__PURE__*/React.createElement(AIEditIcon, 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.createElement("div", {
    "data-testid": testId
  }, /*#__PURE__*/React.createElement(WidthObserver, {
    setWidth: setContainerWidth,
    offscreen: true
  }), /*#__PURE__*/React.createElement("div", {
    className: ax([styles.actionGroup])
  }, promptActions));
};
export default EmbedRovoActionsFooter;