"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Button = void 0;

var _propTypes = _interopRequireDefault(require("prop-types"));

var _analytics = require("../analytics");

var _react = _interopRequireWildcard(require("react"));

var _flags = require("../flags");

var _classnames = _interopRequireDefault(require("classnames"));

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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; }

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 = (0, _react.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 = (0, _classnames.default)('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 (!(0, _flags.buttonSendsAnalytics)() || analytics === false) {
      return;
    }

    var buttonText = analyticsLabelOverride !== null && analyticsLabelOverride !== void 0 ? analyticsLabelOverride : (0, _analytics.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 (0, _analytics.sendLinkEvent)(_objectSpread({
      event_name: 'button_engagement',
      event_type: _analytics.EventCategory.UI_INTERACTION,
      ga_eventCategory: _analytics.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.default.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);
};

exports.Button = Button;
Button.propTypes = {
  /**
     * Analytics events tracking is enabled by default. Set this value to `false` to
     * disable tracking for this component instance.
     */
  analytics: _propTypes.default.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: _propTypes.default.string,

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

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

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

  /**
     * Additional classes to be added to the root button element.
     * Useful for adding utility classes.
     */
  className: _propTypes.default.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: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.elementType]),
  disabled: _propTypes.default.bool,

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

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

  /**
     * Returns the [`SyntheticEvent`](https://facebook.github.io/react/docs/events.html).
     * Not called when the button is disabled.
     */
  onClick: _propTypes.default.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: _propTypes.default.bool,
  size: _propTypes.default.oneOf(['small', 'big']),

  /**
     * A string corresponding to Button variation classes.
     */
  variation: _propTypes.default.oneOf(['solid', 'ghost'])
};
var _default = Button;
exports.default = _default;