"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.useDialogAnalytics = useDialogAnalytics;
var _analytics = require("../analytics");
var _config = require("../config");
var _useNativeDialogAnalytics = require("../NativeDialog/useNativeDialogAnalytics");
function useDialogAnalytics(_ref) {
  let {
    analytics,
    analyticsLabelOverride,
    onAnalyticsEvent = (0, _config.config)().defaultAnalyticsFunction,
    isOpen
  } = _ref;
  function sendDialogEvent(content, eventAttributes) {
    if (analytics !== true && (!(0, _config.config)().dialogSendsAnalytics || analytics === false)) {
      return;
    }
    const eventHeadingText = analyticsLabelOverride ?? content;
    if (!eventHeadingText) {
      console.error('No content found for Dialog analytics event');
      return;
    }
    onAnalyticsEvent({
      event_extension: _analytics.eventExtensionText,
      heading: eventHeadingText,
      ...eventAttributes
    });
  }

  // We need to send modal_impression when it's open once and only once.
  // We need to send modal_closed only when it was open and then closed.
  const headingRef = (0, _useNativeDialogAnalytics.useNativeDialogAnalytics)({
    isOpen,
    onOpen: content => {
      sendDialogEvent(content, {
        event_name: 'modal_impression'
      });
    },
    onClose: content => {
      sendDialogEvent(content, {
        event_name: 'modal_closed'
      });
    }
  });
  return headingRef;
}
var _default = exports.default = useDialogAnalytics;