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

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getPrimitivesPaddingSpaceBySize = exports.default = void 0;
require("./index.compiled.css");
var _react = _interopRequireWildcard(require("react"));
var React = _react;
var _runtime = require("@compiled/react/runtime");
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _reactMagneticDi = require("react-magnetic-di");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _constants = require("../../../../../constants");
var _flexibleUiContext = require("../../../../../state/flexible-ui-context");
var Actions = _interopRequireWildcard(require("../../actions"));
var _actionFooter = require("./action-footer");
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 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
 */
var getPrimitivesPaddingSpaceBySize = exports.getPrimitivesPaddingSpaceBySize = function getPrimitivesPaddingSpaceBySize(size) {
  switch (size) {
    case _constants.SmartLinkSize.XLarge:
      return "var(--ds-space-300, 24px)";
    case _constants.SmartLinkSize.Large:
      return "var(--ds-space-250, 20px)";
    case _constants.SmartLinkSize.Medium:
      return "var(--ds-space-200, 16px)";
    case _constants.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 = (0, _flexibleUiContext.useFlexibleUiContext)();
  var ui = (0, _flexibleUiContext.useFlexibleUiOptionContext)();
  var isRovoChatActionAvailable = (0, _platformFeatureFlags.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[_constants.InternalActionName.RovoChatAction]) === true : undefined;
  var _useState = (0, _react.useState)(),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    message = _useState2[0],
    setMessage = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    isLoading = _useState4[0],
    setIsLoading = _useState4[1];
  var onLoadingChange = (0, _react.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) || _constants.SmartLinkSize.Medium) : undefined;
  var _onClick = (0, _react.useCallback)(function (name) {
    setMessage(undefined);
    onClickCallback === null || onClickCallback === void 0 || onClickCallback(name);
  }, [onClickCallback]);
  var onError = (0, _react.useCallback)(function (error) {
    setMessage(error);
  }, []);
  var actions = (0, _react.useMemo)(function () {
    if (!(context !== null && context !== void 0 && context.actions)) {
      return;
    }
    var arr = (0, _platformFeatureFlags.fg)('platform_sl_3p_auth_rovo_action_kill_switch') ? isRovoChatActionAvailable ? [_constants.InternalActionName.RovoChatAction] : Object.keys(context.actions).filter(function (name) {
      return name !== _constants.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: (0, _runtime.ax)(["_1e0c1txw _2lx21bp4 _vchhusvi _16jlkb7n _1bsb129b _18u01ivi _2hwxc10g", className])
  }, actions, /*#__PURE__*/React.createElement(_actionFooter.ActionFooter, {
    message: message,
    testId: testId
  })) : null;
};
var _default = exports.default = ActionBlock;