import classNames from 'classnames';
import mergeRefs from '../utilities/mergeRefs';
import useButtonAnalytics from './useButtonAnalytics';

// Collect all the additional properties that one could supply to a button component
// that will be passed down to whatever element or component is being used. This is
// generally permissive in order to keep the typing simple at the expense of being
// more accurate. In a previous version of this, `OtherProps` was generic so that we
// could extract any props from a custom component that is being passed in; however,
// we are deprecating that prop because it's not actually needed and creates
// unnecessary complexity that we have to maintain.
import { jsx as _jsx } from "react/jsx-runtime";
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/button/).
 */
export const Button = props => {
  const {
    analytics,
    analyticsLabelOverride,
    analyticsEventTypeOverride,
    analyticsParentHeading,
    analyticsParentType,
    onAnalyticsEvent,
    children,
    className,
    inputRef,
    isAlternate,
    onClick,
    onDark,
    size,
    variation,
    ...otherProps
  } = props;
  const {
    contentRef,
    sendButtonEvent
  } = useButtonAnalytics(props);
  const ComponentType = props.href ? 'a' : 'button';
  const colorSchemeClass = isAlternate && `ds-c-button--alternate`;
  const modeClass = onDark && `ds-c-button--on-dark`;
  const sizeClass = size && `ds-c-button--${size}`;
  const variationClass = variation && `ds-c-button--${variation}`;
  const allClassNames = classNames('ds-c-button', colorSchemeClass, modeClass, sizeClass, variationClass, className);
  const attrs = {
    ...otherProps,
    className: allClassNames
  };
  if (ComponentType !== 'button') {
    delete attrs.disabled;
    delete attrs.type;
    if (props.disabled) {
      attrs.role = 'link';
      attrs['aria-disabled'] = true;
      delete attrs.href;
    }
  }
  function handleClick(e) {
    if (!props.disabled) {
      sendButtonEvent();
      if (onClick) {
        onClick(e);
      }
    }
  }
  function handleKeyPress(e) {
    // Trigger onClick on space key event for `<a>` elements
    if (e.key === ' ') {
      handleClick(e);
    }
  }
  return /*#__PURE__*/_jsx(ComponentType, {
    ref: mergeRefs([inputRef, contentRef]),
    onClick: handleClick,
    onKeyPress: ComponentType === 'a' ? handleKeyPress : undefined,
    ...attrs,
    children: children
  });
};
Button.defaultProps = {
  isAlternate: false,
  onDark: false,
  type: 'button'
};
export default Button;