"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.eventExtensionText = exports.defaultAnalyticsFunction = exports.MAX_STRING_LENGTH = exports.EventType = exports.EventCategory = void 0;
exports.sendAnalytics = sendAnalytics;
exports.sendLinkEvent = sendLinkEvent;
exports.setDefaultAnalyticsFunction = setDefaultAnalyticsFunction;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
 * Functions for sending events to Tealium/Google Analytics
 * Based on HRA Tool & SEP screener & Coverage Tools analytics service:
 * - hra-tool/src/services/analytics.js
 * - screener/src/assets/js/utils/analytics-util.js
 * - coverage-tools-frontend/src/helpers/objectUtilities.ts
 */
var EventCategory = /*#__PURE__*/function (EventCategory) {
  EventCategory["UI_COMPONENTS"] = "ui components";
  EventCategory["UI_INTERACTION"] = "ui interaction";
  return EventCategory;
}({});
exports.EventCategory = EventCategory;
var EventType = /*#__PURE__*/function (EventType) {
  EventType["CONVERSION"] = "conversion";
  EventType["UI_INTERACTION"] = "ui interaction";
  return EventType;
}({}); // This is the default event 'extension' for events arising from the DS
exports.EventType = EventType;
var eventExtensionText = 'Design system integration';
exports.eventExtensionText = eventExtensionText;
var MAX_STRING_LENGTH = 100;
/**
 * Clip all the string values to the MAX_STRING_LENGTH on an event object in place by mutation
 */
exports.MAX_STRING_LENGTH = MAX_STRING_LENGTH;
function clipStrings(event) {
  for (var key in event) {
    var value = event[key];
    if (typeof value === 'string') {
      event[key] = value.substring(0, MAX_STRING_LENGTH);
    }
  }
  return event;
}
var MAX_RETRIES = 3;
var TIMEOUT = 300;

/**
 * Use existing window.utag.link function to send analytics events. If the function does not
 * exist right away, try again after TIMEOUT milliseconds until we've reached MAX_RETRIES.
 */
function sendAnalytics(eventType, event) {
  var retry = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  // If we were to define this on the window object using `declare global { interface Window { utag: ... } }`
  // that type definition of window.utag can conflict with downstream declarations. This happened before, and
  // our fix is to only have a local type so we can get some type-checking without risk of conflicts. This
  // feature of TypeScript is well intentioned (because if you're using globals, you want to make sure every
  // module agrees on what they are), but in reality this type definition could vary in trivial ways but
  // break a build.
  var utag = window.utag;
  if (utag && utag[eventType]) {
    clipStrings(event);
    try {
      utag[eventType](_objectSpread({
        // Expand the event object to support the properties expected on healthcare.gov
        ga_eventValue: '',
        // default value
        ga_eventAction: event.event_action,
        ga_eventCategory: event.event_category,
        ga_eventLabel: event.event_label
      }, event));
    } catch (error) {
      console.warn('Error sending analytics event: ', error);
    }
  } else {
    if (++retry <= MAX_RETRIES) {
      setTimeout(function () {
        return sendAnalytics(eventType, event, retry);
      }, retry * TIMEOUT);
    }
  }
}
function sendLinkEvent(event) {
  return sendAnalytics('link', event);
}
var defaultAnalyticsFunction = sendLinkEvent;

/**
 * Allows applications to override the default `onAnalyticsEvent` function
 * across the whole system. To override it for a single component instance,
 * use the `onAnalyticsEvent` prop instead.
 */
exports.defaultAnalyticsFunction = defaultAnalyticsFunction;
function setDefaultAnalyticsFunction(analyticsFunction) {
  exports.defaultAnalyticsFunction = defaultAnalyticsFunction = analyticsFunction;
}