/* index.tsx generated by @compiled/babel-plugin v0.38.1 */
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useCallback, useMemo, useState } from 'react';
import { di } from 'react-magnetic-di';
import { fg } from '@atlaskit/platform-feature-flags';
import { InternalActionName, SmartLinkSize } from '../../../../../constants';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../state/flexible-ui-context';
import * as Actions from '../../actions';
import { ActionFooter } from './action-footer';
var ignoreContainerPaddingStyles = null;
var DEFAULT_SORT_ORDER = ['PreviewAction', 'CopyLinkAction', 'AISummaryAction'];
var sort = function sort(a, b) {
  var idxA = DEFAULT_SORT_ORDER.indexOf(a);
  var idxB = DEFAULT_SORT_ORDER.indexOf(b);
  if (idxA === -1) {
    idxA = DEFAULT_SORT_ORDER.length;
  }
  if (idxB === -1) {
    idxB = DEFAULT_SORT_ORDER.length;
  }
  return idxA - idxB;
};

/**
 * Get container padding based on smart link size
 * To replace container/index.tsx getPadding() with space token for primitives
 */
export var getPrimitivesPaddingSpaceBySize = function getPrimitivesPaddingSpaceBySize(size) {
  switch (size) {
    case SmartLinkSize.XLarge:
      return "var(--ds-space-300, 24px)";
    case SmartLinkSize.Large:
      return "var(--ds-space-250, 20px)";
    case SmartLinkSize.Medium:
      return "var(--ds-space-200, 16px)";
    case SmartLinkSize.Small:
    default:
      return "var(--ds-space-100, 8px)";
  }
};
var ActionBlock = function ActionBlock(_ref) {
  var _context$actions;
  var blockRef = _ref.blockRef,
    onClickCallback = _ref.onClick,
    size = _ref.size,
    spaceInline = _ref.spaceInline,
    className = _ref.className,
    _ref$testId = _ref.testId,
    testId = _ref$testId === void 0 ? 'smart-block-action' : _ref$testId;
  var context = useFlexibleUiContext();
  var ui = useFlexibleUiOptionContext();
  var isRovoChatActionAvailable = fg('platform_sl_3p_auth_rovo_action_kill_switch') ? (context === null || context === void 0 || (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[InternalActionName.RovoChatAction]) === true : undefined;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    message = _useState2[0],
    setMessage = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isLoading = _useState4[0],
    setIsLoading = _useState4[1];
  var onLoadingChange = useCallback(function (isLoading) {
    setIsLoading(isLoading);
  }, [setIsLoading]);
  var padding = !(ui !== null && ui !== void 0 && ui.hidePadding) ? getPrimitivesPaddingSpaceBySize((ui === null || ui === void 0 ? void 0 : ui.size) || SmartLinkSize.Medium) : undefined;
  var _onClick = useCallback(function (name) {
    setMessage(undefined);
    onClickCallback === null || onClickCallback === void 0 || onClickCallback(name);
  }, [onClickCallback]);
  var onError = useCallback(function (error) {
    setMessage(error);
  }, []);
  var actions = useMemo(function () {
    if (!(context !== null && context !== void 0 && context.actions)) {
      return;
    }
    var arr = fg('platform_sl_3p_auth_rovo_action_kill_switch') ? isRovoChatActionAvailable ? [InternalActionName.RovoChatAction] : Object.keys(context.actions).filter(function (name) {
      return name !== InternalActionName.RovoChatAction;
    }) : Object.keys(context.actions);
    arr.sort(sort);
    return arr.map(function (name) {
      var Action = name in Actions ? Actions[name] : undefined;
      return Action ? /*#__PURE__*/React.createElement(Action, {
        as: "stack-item",
        spaceInline: spaceInline,
        key: name,
        onClick: function onClick() {
          return _onClick(name);
        },
        onError: onError,
        onLoadingChange: onLoadingChange,
        size: size || (ui === null || ui === void 0 ? void 0 : ui.size)
        // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop, @atlaskit/design-system/no-unsafe-design-token-usage
        ,
        style: padding && {
          paddingInline: padding
        },
        hideTooltip: isLoading
      }) : null;
    });
  }, [context === null || context === void 0 ? void 0 : context.actions, isRovoChatActionAvailable, spaceInline, onError, onLoadingChange, size, ui === null || ui === void 0 ? void 0 : ui.size, padding, isLoading, _onClick]);
  return actions ? /*#__PURE__*/React.createElement("div", {
    ref: blockRef,
    "data-testid": testId
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
    ,
    className: ax(["_1e0c1txw _2lx21bp4 _vchhusvi _16jlkb7n _1bsb129b _18u01ivi _2hwxc10g", className])
  }, actions, /*#__PURE__*/React.createElement(ActionFooter, {
    message: message,
    testId: testId
  })) : null;
};
export default ActionBlock;