"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Button = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _mergeRefs = _interopRequireDefault(require("../utilities/mergeRefs"));
var _useButtonAnalytics = _interopRequireDefault(require("./useButtonAnalytics"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// 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.

/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/button/).
 */
const Button = props => {
  const {
    analytics,
    analyticsLabelOverride,
    analyticsEventTypeOverride,
    analyticsParentHeading,
    analyticsParentType,
    onAnalyticsEvent,
    children,
    className,
    inputRef,
    isAlternate,
    onClick,
    onDark,
    size,
    variation,
    ...otherProps
  } = props;
  const {
    contentRef,
    sendButtonEvent
  } = (0, _useButtonAnalytics.default)(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 = (0, _classnames.default)('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__*/(0, _jsxRuntime.jsx)(ComponentType, {
    ref: (0, _mergeRefs.default)([inputRef, contentRef]),
    onClick: handleClick,
    onKeyPress: ComponentType === 'a' ? handleKeyPress : undefined,
    ...attrs,
    children: children
  });
};
exports.Button = Button;
Button.defaultProps = {
  isAlternate: false,
  onDark: false,
  type: 'button'
};
var _default = Button;
exports.default = _default;