/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["triggerRef"];
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
/* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */

import { useCallback, useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { di } from 'react-magnetic-di';
import ButtonGroup from '@atlaskit/button/button-group';
import Button from '@atlaskit/button/standard-button';
import DropdownMenu from '@atlaskit/dropdown-menu';
import MoreIcon from '@atlaskit/icon/core/show-more-horizontal';
import Tooltip from '@atlaskit/tooltip';
import { ActionName, CardDisplay, SmartLinkSize } from '../../../../../constants';
import { messages } from '../../../../../messages';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../state/flexible-ui-context';
import { isBlockCardRovoActionExperimentEnabled } from '../../../../../state/hooks/use-block-card-rovo-action-experiment';
import useRovoConfig from '../../../../../state/hooks/use-rovo-config';
import { RovoChatPromptKey } from '../../../../common/rovo-chat-utils';
import { sizeToButtonSpacing } from '../../utils';
import { filterActionItems } from '../utils';
import ActionGroupItem from './action-group-item';
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] : 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, {
      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 ? 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 = useFlexibleUiContext();
  var ui = useFlexibleUiOptionContext();
  var _useRovoConfig = useRovoConfig(),
    product = _useRovoConfig.product;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isOpen = _useState2[0],
    setIsOpen = _useState2[1];
  var renderableActionItems = useMemo(function () {
    return 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[ActionName.RovoChatAction];
  var is3PBlockExperimentEnabled = isBlockCardRovoActionExperimentEnabled(rovoChatAction === null || rovoChatAction === void 0 ? void 0 : rovoChatAction.product);
  var onOpenChange = useCallback(function (attrs) {
    setIsOpen(attrs.isOpen);
    if (onDropdownOpenChange) {
      onDropdownOpenChange(attrs.isOpen);
    }
  }, [onDropdownOpenChange]);
  var onActionItemClick = useCallback(function () {
    if (isOpen) {
      onOpenChange({
        isOpen: false
      });
    }
  }, [isOpen, onOpenChange]);
  var actionButtons = useMemo(function () {
    if (!!rovoChatAction && is3PBlockExperimentEnabled) {
      var rovoActions = [].concat(_toConsumableArray(containerWidth >= REDUCED_ACTIONS_SIZE ? renderableActionItems.slice(0, visibleButtonsNum - 1) : []), _toConsumableArray(containerWidth >= OVERFLOW_ONLY_ACTIONS_SIZE ? [{
        name: ActionName.RovoChatAction,
        prompts: [RovoChatPromptKey.ASK_ROVO_ANYTHING],
        iconSize: 'small',
        cardAppearance: CardDisplay.Block,
        hideContent: containerWidth < FULL_ACTIONS_SIZE && containerWidth >= REDUCED_ACTIONS_SIZE || containerWidth < ICON_ONLY_ACTIONS_SIZE && containerWidth >= OVERFLOW_ONLY_ACTIONS_SIZE
      }, {
        name: ActionName.CopyLinkAction,
        hideContent: true,
        iconSize: 'small'
      }, {
        name: 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 = useMemo(function () {
    var actionItems;
    if (!!rovoChatAction && is3PBlockExperimentEnabled && containerWidth < OVERFLOW_ONLY_ACTIONS_SIZE) {
      actionItems = [].concat(_toConsumableArray(renderableActionItems), [{
        name: ActionName.RovoChatAction,
        prompts: [RovoChatPromptKey.ASK_ROVO_ANYTHING],
        iconSize: 'small',
        cardAppearance: CardDisplay.Block
      }, {
        name: ActionName.CopyLinkAction,
        iconSize: 'small'
      }, {
        name: 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 = sizeToButtonSpacing[size];
      var moreIcon = /*#__PURE__*/React.createElement(MoreIcon, {
        label: "more",
        color: "currentColor"
      });
      var formatMessage = /*#__PURE__*/React.createElement(FormattedMessage, messages.more_actions);
      return /*#__PURE__*/React.createElement(DropdownMenu, {
        isOpen: isOpen,
        onOpenChange: onOpenChange,
        trigger: function trigger(_ref2) {
          var triggerRef = _ref2.triggerRef,
            props = _objectWithoutProperties(_ref2, _excluded);
          return /*#__PURE__*/React.createElement(Tooltip, {
            content: formatMessage,
            hideTooltipOnClick: true,
            testId: "action-group-more-button-tooltip",
            tag: "span"
          }, /*#__PURE__*/React.createElement(Button, _extends({}, props, {
            spacing: !!rovoChatAction && is3PBlockExperimentEnabled ? size === 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: ax(["_1e0c116y _dzc21h6o _1ilq1i6y", "actions-button-group"]),
    "data-action-open": isOpen
  }, /*#__PURE__*/React.createElement(ButtonGroup, null, actionButtons, moreActionDropdown)) : null;
};
export default ActionGroup;