import { forwardRef, useCallback } from 'react';
import { clsx } from 'clsx';
import { useIntl } from 'react-intl';
import messages from '../../../i18n/commonMessages/Button.messages.mjs';
import { jsx } from 'react/jsx-runtime';

/* eslint-disable react/button-has-type */
const PrimitiveButton = /*#__PURE__*/forwardRef(({
  children,
  className,
  id,
  disabled = false,
  loading = false,
  testId,
  type = 'button',
  onClick,
  onFocus,
  onBlur,
  onMouseEnter,
  onMouseLeave,
  onKeyDown,
  ...props
}, ref) => {
  const intl = useIntl();
  const classNames = clsx('wds-Button', {
    'wds-Button--disabled': disabled,
    'wds-Button--loading': loading
  }, className);
  const handleClick = useCallback(event => {
    if (disabled || loading) {
      event.preventDefault();
    } else {
      onClick?.(event);
    }
  }, [disabled, loading, onClick]);
  const handleFocus = useCallback(event => {
    onFocus?.(event);
  }, [onFocus]);
  const handleBlur = useCallback(event => {
    onBlur?.(event);
  }, [onBlur]);
  const handleMouseEnter = useCallback(event => {
    onMouseEnter?.(event);
  }, [onMouseEnter]);
  const handleMouseLeave = useCallback(event => {
    onMouseLeave?.(event);
  }, [onMouseLeave]);
  const handleKeyDown = 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': disabled || loading,
    'aria-label': loading ? intl.formatMessage(messages.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__*/jsx("button", {
    ...buttonProps,
    children: children
  });
});
PrimitiveButton.displayName = 'PrimitiveButton';

export { PrimitiveButton as default };
//# sourceMappingURL=PrimitiveButton.mjs.map
