/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
"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 _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _reactIntl = require("react-intl");
var _reactMagneticDi = require("react-magnetic-di");
var _buttonGroup = _interopRequireDefault(require("@atlaskit/button/button-group"));
var _standardButton = _interopRequireDefault(require("@atlaskit/button/standard-button"));
var _dropdownMenu = _interopRequireDefault(require("@atlaskit/dropdown-menu"));
var _showMoreHorizontal = _interopRequireDefault(require("@atlaskit/icon/core/show-more-horizontal"));
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _constants = require("../../../../../constants");
var _messages = require("../../../../../messages");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var _useBlockCardRovoActionExperiment = require("../../../../../state/hooks/use-block-card-rovo-action-experiment");
var _useRovoConfig2 = _interopRequireDefault(require("../../../../../state/hooks/use-rovo-config"));
var _rovoChatUtils = require("../../../../common/rovo-chat-utils");
var _utils = require("../../utils");
var _utils2 = require("../utils");
var _actionGroupItem = _interopRequireDefault(require("./action-group-item"));
var _excluded = ["triggerRef"];
/* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */
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); }
var styles = null;
var FULL_ACTIONS_SIZE = 450;
var REDUCED_ACTIONS_SIZE = 360;
var ICON_ONLY_ACTIONS_SIZE = 200;
var OVERFLOW_ONLY_ACTIONS_SIZE = 100;
var renderActionItems = function renderActionItems() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var size = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.SmartLinkSize.Medium;
  var appearance = arguments.length > 2 ? arguments[2] : undefined;
  var asDropDownItems = arguments.length > 3 ? arguments[3] : undefined;
  var onActionItemClick = arguments.length > 4 ? arguments[4] : undefined;
  return items.map(function (item, idx) {
    return /*#__PURE__*/React.createElement(_actionGroupItem.default, {
      item: item,
      key: idx,
      size: size,
      appearance: appearance,
      asDropDownItems: asDropDownItems,
      onActionItemClick: onActionItemClick
    });
  });
};

/**
 * Creates a group of Action components. Accepts an array of Actions, in addition to some styling
 * preferences.
 * @internal
 * @param {ActionGroupProps} ActionGroupProps
 * @see Action
 */
var ActionGroup = function ActionGroup(_ref) {
  var _context$actions;
  var _ref$items = _ref.items,
    items = _ref$items === void 0 ? [] : _ref$items,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? _constants.SmartLinkSize.Medium : _ref$size,
    appearance = _ref.appearance,
    _ref$visibleButtonsNu = _ref.visibleButtonsNum,
    visibleButtonsNum = _ref$visibleButtonsNu === void 0 ? 2 : _ref$visibleButtonsNu,
    onDropdownOpenChange = _ref.onDropdownOpenChange,
    _ref$containerWidth = _ref.containerWidth,
    containerWidth = _ref$containerWidth === void 0 ? Infinity : _ref$containerWidth;
  var context = (0, _flexibleUiContext.useFlexibleUiContext)();
  var ui = (0, _flexibleUiContext.useFlexibleUiOptionContext)();
  var _useRovoConfig = (0, _useRovoConfig2.default)(),
    product = _useRovoConfig.product;
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    isOpen = _useState2[0],
    setIsOpen = _useState2[1];
  var renderableActionItems = (0, _react.useMemo)(function () {
    return (0, _utils2.filterActionItems)(items, context, product);
  }, [context, items, product]);
  var isMoreThenTwoItems = renderableActionItems.length > visibleButtonsNum;
  var rovoChatAction = context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[_constants.ActionName.RovoChatAction];
  var is3PBlockExperimentEnabled = (0, _useBlockCardRovoActionExperiment.isBlockCardRovoActionExperimentEnabled)(rovoChatAction === null || rovoChatAction === void 0 ? void 0 : rovoChatAction.product);
  var onOpenChange = (0, _react.useCallback)(function (attrs) {
    setIsOpen(attrs.isOpen);
    if (onDropdownOpenChange) {
      onDropdownOpenChange(attrs.isOpen);
    }
  }, [onDropdownOpenChange]);
  var onActionItemClick = (0, _react.useCallback)(function () {
    if (isOpen) {
      onOpenChange({
        isOpen: false
      });
    }
  }, [isOpen, onOpenChange]);
  var actionButtons = (0, _react.useMemo)(function () {
    if (!!rovoChatAction && is3PBlockExperimentEnabled) {
      var rovoActions = [].concat((0, _toConsumableArray2.default)(containerWidth >= REDUCED_ACTIONS_SIZE ? renderableActionItems.slice(0, visibleButtonsNum - 1) : []), (0, _toConsumableArray2.default)(containerWidth >= OVERFLOW_ONLY_ACTIONS_SIZE ? [{
        name: _constants.ActionName.RovoChatAction,
        prompts: [_rovoChatUtils.RovoChatPromptKey.ASK_ROVO_ANYTHING],
        iconSize: 'small',
        cardAppearance: _constants.CardDisplay.Block,
        hideContent: containerWidth < FULL_ACTIONS_SIZE && containerWidth >= REDUCED_ACTIONS_SIZE || containerWidth < ICON_ONLY_ACTIONS_SIZE && containerWidth >= OVERFLOW_ONLY_ACTIONS_SIZE
      }, {
        name: _constants.ActionName.CopyLinkAction,
        hideContent: true,
        iconSize: 'small'
      }, {
        name: _constants.ActionName.PreviewAction,
        hideContent: true,
        iconSize: 'small'
      }] : []));
      return renderActionItems(rovoActions, size, appearance, false, onActionItemClick);
    }
    var actionItems = isMoreThenTwoItems ? renderableActionItems.slice(0, visibleButtonsNum - 1) : renderableActionItems;
    return renderActionItems(actionItems, size, appearance, false, onActionItemClick);
  }, [appearance, isMoreThenTwoItems, onActionItemClick, renderableActionItems, size, visibleButtonsNum, rovoChatAction, is3PBlockExperimentEnabled, containerWidth]);
  var moreActionDropdown = (0, _react.useMemo)(function () {
    var actionItems;
    if (!!rovoChatAction && is3PBlockExperimentEnabled && containerWidth < OVERFLOW_ONLY_ACTIONS_SIZE) {
      actionItems = [].concat((0, _toConsumableArray2.default)(renderableActionItems), [{
        name: _constants.ActionName.RovoChatAction,
        prompts: [_rovoChatUtils.RovoChatPromptKey.ASK_ROVO_ANYTHING],
        iconSize: 'small',
        cardAppearance: _constants.CardDisplay.Block
      }, {
        name: _constants.ActionName.CopyLinkAction,
        iconSize: 'small'
      }, {
        name: _constants.ActionName.PreviewAction,
        iconSize: 'small'
      }]);
    } else if (!!rovoChatAction && is3PBlockExperimentEnabled && containerWidth < REDUCED_ACTIONS_SIZE) {
      actionItems = renderableActionItems;
    } else {
      actionItems = isMoreThenTwoItems ? renderableActionItems.slice(visibleButtonsNum - 1) : [];
    }
    if (actionItems.length > 0) {
      var spacing = _utils.sizeToButtonSpacing[size];
      var moreIcon = /*#__PURE__*/React.createElement(_showMoreHorizontal.default, {
        label: "more",
        color: "currentColor"
      });
      var formatMessage = /*#__PURE__*/React.createElement(_reactIntl.FormattedMessage, _messages.messages.more_actions);
      return /*#__PURE__*/React.createElement(_dropdownMenu.default, {
        isOpen: isOpen,
        onOpenChange: onOpenChange,
        trigger: function trigger(_ref2) {
          var triggerRef = _ref2.triggerRef,
            props = (0, _objectWithoutProperties2.default)(_ref2, _excluded);
          return /*#__PURE__*/React.createElement(_tooltip.default, {
            content: formatMessage,
            hideTooltipOnClick: true,
            testId: "action-group-more-button-tooltip",
            tag: "span"
          }, /*#__PURE__*/React.createElement(_standardButton.default, (0, _extends2.default)({}, props, {
            spacing: !!rovoChatAction && is3PBlockExperimentEnabled ? size === _constants.SmartLinkSize.XLarge ? 'default' : 'compact' : spacing,
            testId: "action-group-more-button",
            iconBefore: moreIcon,
            ref: triggerRef
          }, is3PBlockExperimentEnabled ? {
            appearance: appearance
          } : {})));
        },
        testId: "action-group-dropdown",
        zIndex: ui === null || ui === void 0 ? void 0 : ui.zIndex
      }, renderActionItems(actionItems, size, appearance, true, onActionItemClick));
    }
    return null;
  }, [appearance, isMoreThenTwoItems, isOpen, onActionItemClick, onOpenChange, renderableActionItems, size, ui === null || ui === void 0 ? void 0 : ui.zIndex, visibleButtonsNum, rovoChatAction, is3PBlockExperimentEnabled, containerWidth]);
  return renderableActionItems.length > 0 ? /*#__PURE__*/React.createElement("div", {
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
    className: (0, _runtime.ax)(["_1e0c116y _dzc21h6o _1ilq1i6y", "actions-button-group"]),
    "data-action-open": isOpen
  }, /*#__PURE__*/React.createElement(_buttonGroup.default, null, actionButtons, moreActionDropdown)) : null;
};
var _default = exports.default = ActionGroup;