/* index.tsx generated by @compiled/babel-plugin v0.40.0 */
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 { ActionName, SmartLinkSize } from '../../../../../constants';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../state/flexible-ui-context';
import * as Actions from '../../actions';
import { ActionFooter } from './action-footer';
const ignoreContainerPaddingStyles = null;
const DEFAULT_SORT_ORDER = ['PreviewAction', 'CopyLinkAction', 'AISummaryAction'];
const sort = (a, b) => {
  let idxA = DEFAULT_SORT_ORDER.indexOf(a);
  let 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 const 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)";
  }
};
const ActionBlock = ({
  blockRef,
  onClick: onClickCallback,
  size,
  spaceInline,
  className,
  testId = 'smart-block-action'
}) => {
  const context = useFlexibleUiContext();
  const ui = useFlexibleUiOptionContext();
  const [message, setMessage] = useState();
  const [isLoading, setIsLoading] = useState(false);
  const onLoadingChange = useCallback(isLoading => {
    setIsLoading(isLoading);
  }, [setIsLoading]);
  const padding = !(ui !== null && ui !== void 0 && ui.hidePadding) ? getPrimitivesPaddingSpaceBySize((ui === null || ui === void 0 ? void 0 : ui.size) || SmartLinkSize.Medium) : undefined;
  const onClick = useCallback(name => {
    setMessage(undefined);
    onClickCallback === null || onClickCallback === void 0 ? void 0 : onClickCallback(name);
  }, [onClickCallback]);
  const onError = useCallback(error => {
    setMessage(error);
  }, []);
  const actions = useMemo(() => {
    if (!(context !== null && context !== void 0 && context.actions)) {
      return;
    }
    const arr = Object.keys(context.actions).filter(name => name !== ActionName.RovoChatAction);
    arr.sort(sort);
    const renderAction = name => {
      const Action = name in Actions ? Actions[name] : undefined;
      if (!Action) {
        return null;
      }
      return /*#__PURE__*/React.createElement(Action, {
        as: "stack-item",
        spaceInline: spaceInline,
        key: name,
        onClick: () => 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
        ,
        style: padding ? {
          paddingInline: padding
        } : undefined,
        hideTooltip: isLoading
      });
    };
    return arr.map(name => renderAction(name));
  }, [context === null || context === void 0 ? void 0 : context.actions, spaceInline, onError, onLoadingChange, size, ui === null || ui === void 0 ? void 0 : ui.size, padding, isLoading, onClick]);
  if (!actions) {
    return null;
  }
  return /*#__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
  }));
};
export default ActionBlock;