"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.useThirdPartyExternalLinkAnalytics = useThirdPartyExternalLinkAnalytics;
var _analytics = require("../analytics");
var _config = require("../config");
var _compat = require("preact/compat");
function useThirdPartyExternalLinkAnalytics(_ref) {
  let {
    analytics,
    analyticsLabelOverride,
    analyticsParentHeading,
    analyticsParentType,
    onAnalyticsEvent = (0, _config.config)().defaultAnalyticsFunction,
    href
  } = _ref;
  const contentRef = (0, _compat.useRef)();
  function buttonAnalyticsHandler() {
    if (analytics !== true && (!(0, _config.config)().thirdPartyExternalLinkSendsAnalytics || analytics === false)) {
      return;
    }
    const linkContent = analyticsLabelOverride ?? (0, _analytics.getAnalyticsContentFromRefs)([contentRef]);
    if (!linkContent) {
      console.error('No content found for Dialog analytics event');
      return;
    }
    const linkParentHeading = analyticsParentHeading ?? ' ';
    const linkParentType = analyticsParentType ?? ' ';
    onAnalyticsEvent({
      event_name: 'external_link_click',
      event_extension: _analytics.eventExtensionText,
      text: linkContent,
      link_type: 'link_external',
      link_url: href,
      parent_component_heading: linkParentHeading,
      parent_component_type: linkParentType
    });
  }
  return {
    contentRef,
    buttonAnalyticsHandler
  };
}
var _default = exports.default = useThirdPartyExternalLinkAnalytics;