/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
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 { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { ActionName, SmartLinkSize } from '../../../../../constants';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../state/flexible-ui-context';
import * as Actions from '../../actions';
import { ActionFooter } from './action-footer';
import AskRovoSectionHeader from './ask-rovo-section-header';
const ignoreContainerPaddingStyles = null;
const inlineActionNudgePillWrapperStyles = null;
const inlineActionNudgeColumnStyles = 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',
  isAny3pRovoActionsExperimentOn
}) => {
  var _context$actions;
  const context = useFlexibleUiContext();
  const ui = useFlexibleUiOptionContext();
  const isRovoChatActionAvailable = (fg('platform_sl_3p_auth_rovo_action_kill_switch') || fg('rovogrowth-640-inline-action-nudge-fg')) && isAny3pRovoActionsExperimentOn ? (context === null || context === void 0 ? void 0 : (_context$actions = context.actions) === null || _context$actions === void 0 ? void 0 : _context$actions[ActionName.RovoChatAction]) !== undefined : undefined;

  // eslint-disable-next-line @atlaskit/platform/no-preconditioning
  const isInlineActionNudgeExperiment = fg('rovogrowth-640-inline-action-nudge-fg') && expValEqualsNoExposure('rovogrowth-640-inline-action-nudge-exp', 'isEnabled', true);
  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 = fg('platform_sl_3p_auth_rovo_action_kill_switch') || fg('rovogrowth-640-inline-action-nudge-fg') ? isRovoChatActionAvailable ? [ActionName.RovoChatAction] : Object.keys(context.actions).filter(name => name !== ActionName.RovoChatAction) : Object.keys(context.actions);
    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: !(isInlineActionNudgeExperiment && isRovoChatActionAvailable) && padding ? {
          paddingInline: padding
        } : undefined,
        hideTooltip: isLoading
      });
    };
    return arr.map(name => renderAction(name));
  }, [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, isInlineActionNudgeExperiment]);
  const showRovoSectionHeader = isInlineActionNudgeExperiment && isRovoChatActionAvailable;
  if (!actions) {
    return null;
  }
  if (isInlineActionNudgeExperiment && isRovoChatActionAvailable) {
    return /*#__PURE__*/React.createElement("div", {
      ref: blockRef,
      "data-testid": testId,
      className: ax(["_1e0c1txw _2lx21bp4 _vchhusvi _1bsb1osq"])
    }, showRovoSectionHeader && /*#__PURE__*/React.createElement(AskRovoSectionHeader, {
      testId: testId ? `${testId}-rovo-section-header` : undefined
    }), /*#__PURE__*/React.createElement("div", {
      className: ax(["_zulpu2gc _1e0c11p5 _yv0e1ris"])
    }, actions), /*#__PURE__*/React.createElement(ActionFooter, {
      message: message,
      testId: testId
    }));
  }
  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;