import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _typeof from "@babel/runtime/helpers/typeof";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
var _excluded = ["buttonStyles"];
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 React, { forwardRef, useCallback, useEffect, useState } from 'react';
import Button from '@atlaskit/button/custom-theme-button';
import { fg } from '@atlaskit/platform-feature-flags';
import { componentWithCondition } from '@atlaskit/platform-feature-flags-react';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import Tooltip from '@atlaskit/tooltip';
import { Pulse } from '../Pulse/Pulse';
import { ButtonSpotlightCard } from './ButtonSpotlightCard';
import { getButtonStyles, iconOnlySpacing } from './styles';
var customSizeAndPadding = {
  minWidth: "var(--ds-space-400, 32px)",
  padding: "0px ".concat("var(--ds-space-050, 4px)")
};
var FloatingToolbarButton = function FloatingToolbarButton(_ref, forwardedRef) {
  var title = _ref.title,
    icon = _ref.icon,
    iconAfter = _ref.iconAfter,
    onClick = _ref.onClick,
    onKeyDown = _ref.onKeyDown,
    onMouseEnter = _ref.onMouseEnter,
    onMouseLeave = _ref.onMouseLeave,
    onFocus = _ref.onFocus,
    onBlur = _ref.onBlur,
    onMount = _ref.onMount,
    onUnmount = _ref.onUnmount,
    selected = _ref.selected,
    disabled = _ref.disabled,
    href = _ref.href,
    target = _ref.target,
    _ref$appearance = _ref.appearance,
    appearance = _ref$appearance === void 0 ? 'subtle' : _ref$appearance,
    children = _ref.children,
    className = _ref.className,
    tooltipContent = _ref.tooltipContent,
    tooltipStyle = _ref.tooltipStyle,
    testId = _ref.testId,
    interactionName = _ref.interactionName,
    _ref$hideTooltipOnCli = _ref.hideTooltipOnClick,
    hideTooltipOnClick = _ref$hideTooltipOnCli === void 0 ? true : _ref$hideTooltipOnCli,
    ariaHasPopup = _ref.ariaHasPopup,
    tabIndex = _ref.tabIndex,
    areaControls = _ref.areaControls,
    ariaLabel = _ref.ariaLabel,
    isRadioButton = _ref.isRadioButton,
    pulse = _ref.pulse,
    spotlightConfig = _ref.spotlightConfig,
    areAnyNewToolbarFlagsEnabled = _ref.areAnyNewToolbarFlagsEnabled;
  // Check if there's only an icon and add additional styles
  var iconOnly = (icon || iconAfter) && !children;
  var customSpacing = iconOnly ? iconOnlySpacing : {};
  var isButtonPressed = ariaHasPopup ? undefined : selected;
  /**
   * If it's a radio button, we need to reflect false values too, hence
   * we cast it as a Boolean
   */
  var ariaChecked = isRadioButton ? fg('platform_editor_dec_a11y_fixes') ? Boolean(isButtonPressed) : isButtonPressed : undefined;
  var ariaPressed = isRadioButton ? undefined : isButtonPressed;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    spotlightReferenceElement = _useState2[0],
    setSpotlightReferenceElement = _useState2[1];
  useEffect(function () {
    onMount === null || onMount === void 0 || onMount();
    return function () {
      return onUnmount === null || onUnmount === void 0 ? void 0 : onUnmount();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  var onSpotlightTargetClick = spotlightConfig !== null && spotlightConfig !== void 0 && spotlightConfig.isSpotlightOpen ? spotlightConfig === null || spotlightConfig === void 0 ? void 0 : spotlightConfig.onTargetClick : undefined;
  var handleOnClick = useCallback(function (event) {
    // fire the spotlight onTargetClick callback if a spotlight is rendered and callback is provided
    onSpotlightTargetClick === null || onSpotlightTargetClick === void 0 || onSpotlightTargetClick();
    onClick === null || onClick === void 0 || onClick(event);
  }, [onClick, onSpotlightTargetClick]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Tooltip
  // Only fallback to title for tooltip if title is not shown
  , {
    content: tooltipContent || (iconOnly ? title : undefined),
    component: tooltipStyle,
    hideTooltipOnClick: hideTooltipOnClick,
    position: "top"
  }, /*#__PURE__*/React.createElement("div", {
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/React.createElement(Pulse, {
    pulse: pulse || (spotlightConfig === null || spotlightConfig === void 0 ? void 0 : spotlightConfig.pulse)
  }, /*#__PURE__*/React.createElement(Button
  // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop, @atlaskit/design-system/no-unsafe-style-overrides -- Ignored via go/DSP-18766
  , {
    className: className,
    ref: function ref(buttonElement) {
      setSpotlightReferenceElement(buttonElement);
      if (forwardedRef && editorExperiment('platform_synced_block', true)) {
        if (typeof forwardedRef === 'function') {
          forwardedRef(buttonElement);
        } else if (_typeof(forwardedRef) === 'object') {
          forwardedRef.current = buttonElement;
        }
      }
    }
    // eslint-disable-next-line @atlaskit/design-system/no-unsafe-style-overrides
    ,
    theme: function theme(adgTheme, themeProps) {
      var _adgTheme = adgTheme(themeProps),
        buttonStyles = _adgTheme.buttonStyles,
        rest = _objectWithoutProperties(_adgTheme, _excluded);
      return _objectSpread({
        buttonStyles: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, buttonStyles), customSpacing), appearance === 'danger' && getButtonStyles({
          appearance: appearance,
          state: themeProps.state,
          mode: themeProps.mode
        })), areAnyNewToolbarFlagsEnabled ? customSizeAndPadding : {})
      }, rest);
    },
    "aria-label": ariaLabel || title,
    "aria-pressed": ariaPressed,
    "aria-checked": ariaChecked,
    role: isRadioButton ? 'radio' : undefined,
    "aria-expanded": ariaHasPopup ? selected : undefined,
    "aria-controls": ariaHasPopup ? areaControls : undefined,
    spacing: areAnyNewToolbarFlagsEnabled ? 'default' : 'compact',
    href: href,
    target: target,
    appearance: appearance,
    "aria-haspopup": ariaHasPopup,
    iconBefore: icon || undefined,
    iconAfter: iconAfter,
    onClick: handleOnClick,
    onKeyDown: onKeyDown,
    isSelected: selected,
    isDisabled: disabled,
    testId: testId,
    interactionName: interactionName,
    onFocus: onFocus,
    onBlur: onBlur
    // @ts-ignore
    // tabIndex set as 0 by default in the design system  ButtonBase component
    // this is not expected for all buttons, we have to use tabIndex={null} for some cases
    // should be fixed here https://a11y-internal.atlassian.net/browse/DST-287
    ,
    tabIndex: tabIndex
  }, children)))), (spotlightConfig === null || spotlightConfig === void 0 ? void 0 : spotlightConfig.isSpotlightOpen) && spotlightReferenceElement && /*#__PURE__*/React.createElement(ButtonSpotlightCard, _extends({
    referenceElement: spotlightReferenceElement
    // Ignored via go/ees005
    // eslint-disable-next-line react/jsx-props-no-spreading
  }, spotlightConfig.spotlightCardOptions)));
};
export default componentWithCondition(function () {
  return editorExperiment('platform_synced_block', true);
}, /*#__PURE__*/forwardRef(FloatingToolbarButton), FloatingToolbarButton);