"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof3 = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _customThemeButton = _interopRequireDefault(require("@atlaskit/button/custom-theme-button"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _Pulse = require("../Pulse/Pulse");
var _ButtonSpotlightCard = require("./ButtonSpotlightCard");
var _styles = require("./styles");
var _excluded = ["buttonStyles"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
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 ? _styles.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 ? (0, _platformFeatureFlags.fg)('platform_editor_dec_a11y_fixes') ? Boolean(isButtonPressed) : isButtonPressed : undefined;
  var ariaPressed = isRadioButton ? undefined : isButtonPressed;
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    spotlightReferenceElement = _useState2[0],
    setSpotlightReferenceElement = _useState2[1];
  (0, _react.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 = (0, _react.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.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_tooltip.default
  // Only fallback to title for tooltip if title is not shown
  , {
    content: tooltipContent || (iconOnly ? title : undefined),
    component: tooltipStyle,
    hideTooltipOnClick: hideTooltipOnClick,
    position: "top"
  }, /*#__PURE__*/_react.default.createElement("div", {
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/_react.default.createElement(_Pulse.Pulse, {
    pulse: pulse || (spotlightConfig === null || spotlightConfig === void 0 ? void 0 : spotlightConfig.pulse)
  }, /*#__PURE__*/_react.default.createElement(_customThemeButton.default
  // 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 && (0, _experiments.editorExperiment)('platform_synced_block', true)) {
        if (typeof forwardedRef === 'function') {
          forwardedRef(buttonElement);
        } else if ((0, _typeof2.default)(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 = (0, _objectWithoutProperties2.default)(_adgTheme, _excluded);
      return _objectSpread({
        buttonStyles: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, buttonStyles), customSpacing), appearance === 'danger' && (0, _styles.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.default.createElement(_ButtonSpotlightCard.ButtonSpotlightCard, (0, _extends2.default)({
    referenceElement: spotlightReferenceElement
    // Ignored via go/ees005
    // eslint-disable-next-line react/jsx-props-no-spreading
  }, spotlightConfig.spotlightCardOptions)));
};
var _default = exports.default = (0, _platformFeatureFlagsReact.componentWithCondition)(function () {
  return (0, _experiments.editorExperiment)('platform_synced_block', true);
}, /*#__PURE__*/(0, _react.forwardRef)(FloatingToolbarButton), FloatingToolbarButton);