"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _customThemeButton = _interopRequireDefault(require("@atlaskit/button/custom-theme-button"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _styles = require("./styles");
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) { (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 _default = exports.default = function _default(_ref) {
  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,
    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,
    testId = _ref.testId,
    _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;
  // 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;
  var ariaChecked = isRadioButton ? isButtonPressed : undefined;
  var ariaCheckedWithFF = (0, _platformFeatureFlags.getBooleanFF)('platform.editor.a11y-floating-toolbar-markup_vexmo') ? ariaChecked : undefined;
  var ariaPressed = isRadioButton ? undefined : isButtonPressed;
  var ariaPressedWithFF = (0, _platformFeatureFlags.getBooleanFF)('platform.editor.a11y-floating-toolbar-markup_vexmo') ? ariaPressed : isButtonPressed;
  return /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    content: tooltipContent || title,
    hideTooltipOnClick: hideTooltipOnClick,
    position: "top"
  }, /*#__PURE__*/_react.default.createElement("div", {
    onMouseEnter: onMouseEnter,
    onMouseLeave: onMouseLeave
  }, /*#__PURE__*/_react.default.createElement(_customThemeButton.default, {
    className: className,
    theme: function theme(adgTheme, themeProps) {
      var _adgTheme = adgTheme(themeProps),
        buttonStyles = _adgTheme.buttonStyles,
        rest = (0, _objectWithoutProperties2.default)(_adgTheme, _excluded);
      return _objectSpread({
        buttonStyles: _objectSpread(_objectSpread(_objectSpread({}, buttonStyles), customSpacing), appearance === 'danger' && (0, _styles.getButtonStyles)({
          appearance: appearance,
          state: themeProps.state,
          mode: themeProps.mode
        }))
      }, rest);
    },
    "aria-label": ariaLabel || title,
    "aria-pressed": ariaPressedWithFF,
    "aria-checked": ariaCheckedWithFF,
    role: isRadioButton ? 'radio' : undefined,
    "aria-expanded": ariaHasPopup ? selected : undefined,
    "aria-controls": ariaHasPopup ? areaControls : undefined,
    spacing: 'compact',
    href: href,
    target: target,
    appearance: appearance,
    "aria-haspopup": ariaHasPopup,
    iconBefore: icon || undefined,
    iconAfter: iconAfter,
    onClick: onClick,
    onKeyDown: onKeyDown,
    isSelected: selected,
    isDisabled: disabled,
    testId: testId,
    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)));
};