'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var clsx = require('clsx');
var reactIntl = require('react-intl');
var Button_messages = require('../../i18n/commonMessages/Button.messages.js');
var jsxRuntime = require('react/jsx-runtime');

/* eslint-disable react/button-has-type */
const PrimitiveButton = /*#__PURE__*/React.forwardRef(({
  children,
  className,
  id,
  disabled = false,
  loading = false,
  testId,
  type = 'button',
  onClick,
  onFocus,
  onBlur,
  onMouseEnter,
  onMouseLeave,
  onKeyDown,
  ...props
}, ref) => {
  const intl = reactIntl.useIntl();
  const classNames = clsx.clsx(className);
  const handleClick = React.useCallback(event => {
    if (disabled || loading) {
      event.preventDefault();
    } else {
      onClick?.(event);
    }
  }, [disabled, loading, onClick]);
  const handleFocus = React.useCallback(event => {
    onFocus?.(event);
  }, [onFocus]);
  const handleBlur = React.useCallback(event => {
    onBlur?.(event);
  }, [onBlur]);
  const handleMouseEnter = React.useCallback(event => {
    onMouseEnter?.(event);
  }, [onMouseEnter]);
  const handleMouseLeave = React.useCallback(event => {
    onMouseLeave?.(event);
  }, [onMouseLeave]);
  const handleKeyDown = React.useCallback(event => {
    onKeyDown?.(event);
  }, [onKeyDown]);
  /**
   * The following props are used to manage the `disabled` and `loading`
   * states for the button:
   *
   * - `aria-disabled`: Communicates to assistive technologies that the button
   *   is disabled when it is either disabled or loading.
   * - `aria-label`: Provides an accessible label for the button, using a
   *   localized loading message when the button is in a loading state.
   * - `aria-live`: Ensures that updates to the button's content are announced
   *   by assistive technologies, set to 'polite' during loading.
   */
  const buttonProps = {
    'aria-disabled': loading || undefined,
    'aria-busy': loading || undefined,
    'aria-label': loading ? intl.formatMessage(Button_messages.default.loadingAriaLabel) : props['aria-label'],
    'aria-live': loading ? 'polite' : 'off',
    className: classNames,
    'data-testid': testId,
    disabled,
    id,
    ref,
    type,
    onBlur: handleBlur,
    onClick: handleClick,
    onFocus: handleFocus,
    onMouseEnter: handleMouseEnter,
    onMouseLeave: handleMouseLeave,
    onKeyDown: handleKeyDown,
    ...props
  };
  return /*#__PURE__*/jsxRuntime.jsx("button", {
    ...buttonProps,
    children: children
  });
});
PrimitiveButton.displayName = 'PrimitiveButton';

exports.default = PrimitiveButton;
//# sourceMappingURL=PrimitiveButton.js.map
