import _pt from "prop-types";
var _excluded = ["analytics", "analyticsLabelOverride", "analyticsParentHeading", "analyticsParentType", "children", "className", "component", "disabled", "href", "inputRef", "isAlternate", "onClick", "onDark", "size", "variation", "type"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import { EventCategory, sendLinkEvent, getAnalyticsContentFromRefs } from '../analytics';
import React, { useRef } from 'react';
import { buttonSendsAnalytics } from '../flags';
import classNames from 'classnames';
export var Button = function Button(_ref) {
  var analytics = _ref.analytics,
      analyticsLabelOverride = _ref.analyticsLabelOverride,
      analyticsParentHeading = _ref.analyticsParentHeading,
      analyticsParentType = _ref.analyticsParentType,
      children = _ref.children,
      className = _ref.className,
      component = _ref.component,
      disabled = _ref.disabled,
      href = _ref.href,
      inputRef = _ref.inputRef,
      _ref$isAlternate = _ref.isAlternate,
      isAlternate = _ref$isAlternate === void 0 ? false : _ref$isAlternate,
      onClick = _ref.onClick,
      _ref$onDark = _ref.onDark,
      onDark = _ref$onDark === void 0 ? false : _ref$onDark,
      size = _ref.size,
      variation = _ref.variation,
      _ref$type = _ref.type,
      type = _ref$type === void 0 ? 'button' : _ref$type,
      otherProps = _objectWithoutProperties(_ref, _excluded);

  var contentRef = useRef();
  var ComponentType = component !== null && component !== void 0 ? component : href ? 'a' : 'button';
  var colorSchemeClass = isAlternate && "ds-c-button--alternate";
  var modeClass = onDark && "ds-c-button--on-dark";
  var sizeClass = size && "ds-c-button--".concat(size);
  var variationClass = variation && "ds-c-button--".concat(variation);
  var allClassNames = classNames('ds-c-button', colorSchemeClass, modeClass, sizeClass, variationClass, className);

  var attrs = _objectSpread({
    className: allClassNames,
    disabled: disabled,
    href: href,
    type: type
  }, otherProps);

  if (ComponentType !== 'button') {
    // Assume `component` is not a custom component rendering a <button>
    // and remove <button> specific attributes
    delete attrs.disabled;
    delete attrs.type;

    if (disabled) {
      attrs.role = 'link';
      attrs['aria-disabled'] = true;
      delete attrs.href;
    }
  }

  function sendButtonEvent() {
    if (!buttonSendsAnalytics() || analytics === false) {
      return;
    }

    var buttonText = analyticsLabelOverride !== null && analyticsLabelOverride !== void 0 ? analyticsLabelOverride : getAnalyticsContentFromRefs([contentRef]);
    var buttonStyle = variation !== null && variation !== void 0 ? variation : 'default';
    var buttonType = type !== null && type !== void 0 ? type : 'button';
    var buttonParentHeading = analyticsParentHeading !== null && analyticsParentHeading !== void 0 ? analyticsParentHeading : ' ';
    var buttonParentType = analyticsParentType !== null && analyticsParentType !== void 0 ? analyticsParentType : ' ';
    return sendLinkEvent(_objectSpread({
      event_name: 'button_engagement',
      event_type: EventCategory.UI_INTERACTION,
      ga_eventCategory: EventCategory.UI_INTERACTION,
      ga_eventAction: "engaged ".concat(buttonStyle, " button"),
      ga_eventLabel: href ? "".concat(buttonText, ": ").concat(href) : buttonText,
      text: buttonText,
      button_style: buttonStyle,
      button_type: href ? 'link' : buttonType,
      parent_component_heading: buttonParentHeading,
      parent_component_type: buttonParentType
    }, href ? {
      link_url: href
    } : {}));
  }

  function handleClick(e) {
    if (!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__*/React.createElement(ComponentType, _extends({
    ref: function ref(el) {
      contentRef.current = el;

      if (inputRef) {
        if (typeof inputRef === 'function') {
          inputRef(el);
        } else {
          inputRef.current = el;
        }
      }
    },
    onClick: handleClick,
    onKeyPress: ComponentType === 'a' ? handleKeyPress : undefined
  }, attrs), children);
};
Button.propTypes = {
  /**
     * Analytics events tracking is enabled by default. Set this value to `false` to
     * disable tracking for this component instance.
     */
  analytics: _pt.bool,

  /**
     * An override for the dynamic content sent to analytics services. By default this content comes from the heading.
     *
     * In cases where this component’s heading may contain **sensitive information**, use this prop to override what is sent to analytics.
     */
  analyticsLabelOverride: _pt.string,

  /**
     * If needed for analytics, pass heading text of parent component of button.
     */
  analyticsParentHeading: _pt.string,

  /**
     * If needed for analytics, pass type of parent component of button.
     */
  analyticsParentType: _pt.string,

  /**
     * Label text or HTML
     */
  children: _pt.oneOfType([_pt.string, _pt.node]).isRequired,

  /**
     * Additional classes to be added to the root button element.
     * Useful for adding utility classes.
     */
  className: _pt.string,

  /**
     * @hide-prop [Deprecated] Support for rendering custom components will be removed
     * in the next major version. If you need to use React components like react-router
     * `Link`, try wrapping this component instead.
     */
  component: _pt.oneOfType([_pt.node, _pt.elementType]),
  disabled: _pt.bool,

  /**
     * When provided the root component will render as an `<a>` element
     * rather than `button`.
     */
  href: _pt.string,

  /**
     * Applies the alternate color to a Button. By default, Button
     * uses the `main` color.
     */
  isAlternate: _pt.bool,

  /**
     * Returns the [`SyntheticEvent`](https://facebook.github.io/react/docs/events.html).
     * Not called when the button is disabled.
     */
  onClick: _pt.func,

  /**
     * Defines a color palette best used when Button is placed on a dark
     * background-color. By default, Button uses a light color palette.
     */
  onDark: _pt.bool,
  size: _pt.oneOf(['small', 'big']),

  /**
     * A string corresponding to Button variation classes.
     */
  variation: _pt.oneOf(['solid', 'ghost'])
};
export default Button;