/* index.tsx generated by @compiled/babel-plugin v0.39.1 */
import "./index.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { forwardRef, useCallback, useMemo } from 'react';
import { cx } from '@compiled/react';
import { LoadingButton } from '@atlaskit/button';
import Button, { IconButton, LinkButton, LinkIconButton } from '@atlaskit/button/new';
import { Box } from '@atlaskit/primitives/compiled';
import Tooltip from '@atlaskit/tooltip';
import { ActionName, SmartLinkSize } from '../../../../../../constants';
import { useFlexibleUiContext, useFlexibleUiOptionContext } from '../../../../../../state/flexible-ui-context';
import useRovoConfig from '../../../../../../state/hooks/use-rovo-config';
import { withOverrideCss } from '../../../common/with-override-css';
import { sizeToButtonSpacing } from '../../../utils';
var IconOnlyLarge = null;
var SizeSmall = null;
var styles = {
  textSmall: "_13bowadc _1q9t1wq8 _1rlg1g80 _bdn61h6o"
};
var SizeSmallIconOnly = null;
var SizeSmallNotIconOnly = null;
var IconButtonAppearanceMap = {
  default: 'default',
  danger: undefined,
  link: 'default',
  primary: 'primary',
  subtle: 'subtle',
  'subtle-link': 'subtle',
  warning: undefined
};
var ButtonAppearanceMap = {
  default: 'default',
  danger: 'danger',
  link: 'default',
  primary: 'primary',
  subtle: 'subtle',
  'subtle-link': 'subtle',
  warning: 'warning'
};
var tooltipOptions = {
  hideTooltipOnClick: true
};
var ActionButtonRefresh = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var _context$actions;
  var _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'default' : _ref$appearance,
    content = _ref.content,
    iconAfter = _ref.iconAfter,
    iconBefore = _ref.iconBefore,
    isLoading = _ref.isLoading,
    onClick = _ref.onClick,
    size = _ref.size,
    testId = _ref.testId,
    tooltipMessage = _ref.tooltipMessage,
    isDisabled = _ref.isDisabled,
    href = _ref.href,
    ariaLabel = _ref.ariaLabel;
  var iconOnly = !content;
  var context = useFlexibleUiContext();
  var _useRovoConfig = useRovoConfig(),
    product = _useRovoConfig.product;
  var isRovoSupported = !!(context !== null && context !== void 0 && (_context$actions = context.actions) !== null && _context$actions !== void 0 && _context$actions[ActionName.RovoChatAction]) && !!product && product === 'CONFLUENCE';
  var onButtonClick = useCallback(function (handler) {
    return function (e) {
      e.preventDefault();
      handler();
    };
  }, []);
  var button = useMemo(function () {
    var isLinkButton = !!href;
    var spacing = size === SmartLinkSize.Large || size === SmartLinkSize.XLarge ? 'default' : 'compact';
    var icon = iconBefore || iconAfter;
    var iconFn = icon ? function () {
      return icon;
    } : undefined;
    if (iconOnly && iconFn) {
      if (isLinkButton && iconFn) {
        return /*#__PURE__*/React.createElement(LinkIconButton, {
          appearance: IconButtonAppearanceMap[appearance],
          icon: iconFn,
          href: href,
          isDisabled: isDisabled,
          isTooltipDisabled: false,
          label: tooltipMessage,
          onClick: onButtonClick(onClick),
          spacing: spacing,
          testId: testId,
          tooltip: tooltipOptions
        });
      }
      return /*#__PURE__*/React.createElement(IconButton, {
        appearance: IconButtonAppearanceMap[appearance],
        icon: iconFn,
        isDisabled: isDisabled,
        isLoading: isLoading,
        isTooltipDisabled: false,
        label: tooltipMessage,
        onClick: onButtonClick(onClick),
        spacing: spacing,
        testId: testId,
        tooltip: tooltipOptions
      });
    }
    var iconBeforeFn = iconBefore ? function () {
      return iconBefore;
    } : undefined;
    var iconAfterFn = iconAfter ? function () {
      return iconAfter;
    } : undefined;
    if (isLinkButton) {
      return /*#__PURE__*/React.createElement(Tooltip, {
        content: tooltipMessage,
        hideTooltipOnClick: true,
        testId: "".concat(testId, "-tooltip")
      }, /*#__PURE__*/React.createElement(LinkButton, {
        appearance: ButtonAppearanceMap[appearance],
        "aria-label": ariaLabel,
        iconAfter: iconAfterFn,
        iconBefore: iconBeforeFn,
        isDisabled: isDisabled,
        href: href,
        onClick: onButtonClick(onClick),
        spacing: spacing,
        testId: testId
      }, content));
    }
    return /*#__PURE__*/React.createElement(Tooltip, {
      content: tooltipMessage,
      hideTooltipOnClick: true,
      testId: "".concat(testId, "-tooltip")
    }, /*#__PURE__*/React.createElement(Button, {
      appearance: ButtonAppearanceMap[appearance],
      "aria-label": ariaLabel,
      iconAfter: iconAfterFn,
      iconBefore: iconBeforeFn,
      isDisabled: isDisabled,
      isLoading: isLoading,
      onClick: onButtonClick(onClick),
      spacing: spacing,
      testId: testId
    }, content));
  }, [appearance, ariaLabel, content, href, iconAfter, iconBefore, iconOnly, isDisabled, isLoading, onButtonClick, onClick, size, testId, tooltipMessage]);
  return /*#__PURE__*/React.createElement(Box, {
    testId: "".concat(testId, "-button-wrapper"),
    ref: ref,
    xcss: cx(isRovoSupported && size === SmartLinkSize.Small && styles.textSmall)
  }, button);
});
var ActionButtonRefreshWithOverrideCss = withOverrideCss(ActionButtonRefresh);
var ActionButtonNew = function ActionButtonNew(_ref2) {
  var _ref2$appearance = _ref2.appearance,
    appearance = _ref2$appearance === void 0 ? 'default' : _ref2$appearance,
    content = _ref2.content,
    iconAfter = _ref2.iconAfter,
    iconBefore = _ref2.iconBefore,
    isLoading = _ref2.isLoading,
    onClick = _ref2.onClick,
    size = _ref2.size,
    testId = _ref2.testId,
    tooltipMessage = _ref2.tooltipMessage,
    isDisabled = _ref2.isDisabled,
    href = _ref2.href,
    ariaLabel = _ref2.ariaLabel,
    className = _ref2.className;
  var iconOnly = !content;
  var onButtonClick = useCallback(function (handler) {
    return function (e) {
      e.preventDefault();
      handler();
    };
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop
    className: ax([size === SmartLinkSize.Large && iconOnly && "_1gflidpf _or3fidpf _1ddvidpf _1hhaidpf _yhjmidpf _1trnidpf _1fstidpf _1xceidpf _1gqnidpf _1qdmidpf _df2ridpf _j1twidpf _11lvidpf _192bidpf _vbn7idpf _wfu3idpf _1i3bidpf _13gwidpf _4wlcidpf _rehaidpf _mp2fidpf _ro8midpf _1l0qidpf _1bnaidpf _3kh9idpf _i2vxidpf _8pvjidpf _1e1oidpf _1vtwidpf _ijvwidpf _tpwpidpf _1ad8idpf", size === SmartLinkSize.Small && "_11c8wadc _k48p1wq8", size === SmartLinkSize.Small && iconOnly && "_1gflv77o _or3fv77o _1ddvv77o _1hhav77o _yhjmv77o _1trnv77o _1fstv77o _1xcev77o _1gqnv77o _1qdmv77o _df2rv77o _j1twv77o _11lvv77o _192bv77o _vbn7v77o _wfu3v77o", size === SmartLinkSize.Small && !iconOnly && "_11lv1b66 _192b1b66 _vbn71b66 _wfu31b66 _yhjm1b66 _1trn1b66 _1fst1b66 _1xce1b66", className]),
    "data-testid": "".concat(testId, "-button-wrapper")
  }, /*#__PURE__*/React.createElement(Tooltip, {
    content: tooltipMessage,
    hideTooltipOnClick: true,
    testId: "".concat(testId, "-tooltip")
  }, /*#__PURE__*/React.createElement(LoadingButton, {
    appearance: appearance,
    iconAfter: iconAfter,
    iconBefore: iconBefore,
    isDisabled: isDisabled,
    isLoading: isLoading,
    onClick: onButtonClick(onClick),
    spacing: sizeToButtonSpacing[size],
    testId: testId,
    href: href,
    "aria-label": ariaLabel
  }, content)));
};
var ActionButton = function ActionButton(props) {
  var ui = useFlexibleUiOptionContext();
  if (ui !== null && ui !== void 0 && ui.hideLegacyButton || ui !== null && ui !== void 0 && ui.removeBlockRestriction) {
    return /*#__PURE__*/React.createElement(ActionButtonRefreshWithOverrideCss, props);
  }
  return /*#__PURE__*/React.createElement(ActionButtonNew, props);
};
export default ActionButton;