/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "./index.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { useCallback, useMemo, useState } from 'react';
import { di } from 'react-magnetic-di';
import { FadeIn, StaggeredEntrance } from '@atlaskit/motion';
import { fg } from '@atlaskit/platform-feature-flags';
import { InternalActionName, SmartLinkSize } from '../../../../../constants';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../state/flexible-ui-context';
import useAISummaryAction from '../../../../../state/hooks/use-ai-summary-action';
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,
    is3PAuthRovoActionsExperimentOn = _ref.is3PAuthRovoActionsExperimentOn;
  var context = useFlexibleUiContext();
  var ui = useFlexibleUiOptionContext();
  var url = context === null || context === void 0 ? void 0 : context.url;
  var isRovoChatActionAvailable = is3PAuthRovoActionsExperimentOn && 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]) !== undefined : 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);
    var renderAction = function renderAction(name, motionStyle) {
      var Action = name in Actions ? Actions[name] : undefined;
      if (!Action) {
        return null;
      }
      var style = padding || motionStyle ? _objectSpread(_objectSpread({}, motionStyle || {}), padding && {
        paddingInline: padding
      }) : undefined;
      return /*#__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: style,
        hideTooltip: isLoading
      });
    };
    return isRovoChatActionAvailable ? /*#__PURE__*/React.createElement(StaggeredEntrance, {
      columns: 1
    }, arr.map(function (name, index) {
      return /*#__PURE__*/React.createElement(FadeIn, {
        duration: 'large',
        key: index
      }, function (motion) {
        return renderAction(name, motion.style);
      });
    })) : arr.map(function (name) {
      return 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]);
  var aiSummaryConfig = fg('platform_sl_3p_auth_rovo_action_kill_switch') ?
  // eslint-disable-next-line react-hooks/rules-of-hooks
  useAISummaryAction(url !== null && url !== void 0 ? url : '') : undefined;
  if (fg('platform_sl_3p_auth_rovo_action_kill_switch')) {
    var shouldShowActions = !isRovoChatActionAvailable || isRovoChatActionAvailable && ((aiSummaryConfig === null || aiSummaryConfig === void 0 ? void 0 : aiSummaryConfig.state.status) === 'done' || (aiSummaryConfig === null || aiSummaryConfig === void 0 ? void 0 : aiSummaryConfig.state.status) === 'error');
    return actions && shouldShowActions ? /*#__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;
  }
  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;