"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useButtonAnalytics;
var _config = require("../config");
var _analytics = require("../analytics");
var _react = require("react");
function useButtonAnalytics(_ref) {
  let {
    analytics,
    analyticsLabelOverride,
    analyticsParentHeading,
    analyticsParentType,
    href,
    onAnalyticsEvent = (0, _config.config)().defaultAnalyticsFunction,
    type,
    variation
  } = _ref;
  const contentRef = (0, _react.useRef)();
  function sendButtonEvent() {
    if (analytics !== true && (!(0, _config.config)().buttonSendsAnalytics || analytics === false)) {
      return;
    }
    const buttonText = analyticsLabelOverride ?? (0, _analytics.getAnalyticsContentFromRefs)([contentRef]);
    const buttonStyle = variation ?? 'default';
    const buttonType = type ?? 'button';
    const buttonParentHeading = analyticsParentHeading ?? ' ';
    const buttonParentType = analyticsParentType ?? ' ';
    return onAnalyticsEvent({
      event_name: 'button_engagement',
      event_type: _analytics.EventType.UI_INTERACTION,
      event_category: _analytics.EventCategory.UI_INTERACTION,
      event_action: `engaged ${buttonStyle} button`,
      event_label: href ? `${buttonText}: ${href}` : buttonText,
      event_extension: _analytics.eventExtensionText,
      text: buttonText,
      button_style: buttonStyle,
      button_type: href ? 'link' : buttonType,
      parent_component_heading: buttonParentHeading,
      parent_component_type: buttonParentType,
      ...(href ? {
        link_url: href
      } : {})
    });
  }
  return {
    contentRef,
    sendButtonEvent
  };
}