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

/**
 * 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
 */
export var MAX_LENGTH = 100;
export var EventCategory;

(function (EventCategory) {
  EventCategory["UI_COMPONENTS"] = "ui components";
  EventCategory["UI_INTERACTION"] = "ui interaction";
})(EventCategory || (EventCategory = {}));

/**
 * Clip all the string values to the MAX_LENGTH on an event object in place by mutation
 */
function clipStrings(event) {
  for (var key in event) {
    var value = event[key];

    if (typeof value === 'string') {
      event[key] = value.substring(0, MAX_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.
 */

export 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](event);
      return "Tealium event sent: ".concat(event.ga_eventCategory, " - ").concat(event.ga_eventAction, " - ").concat(event.ga_eventLabel);
    } catch (e) {
      return "Error sending event to Tealium ".concat(e);
    }
  } else {
    if (++retry <= MAX_RETRIES) {
      setTimeout(function () {
        return sendAnalytics(eventType, event, retry);
      }, retry * TIMEOUT);
    } else {
      return "Tealium event max retries reached";
    }
  }
}
export function sendLinkEvent(payload) {
  return sendAnalytics('link', _objectSpread({
    ga_eventType: 'cmsds',
    // default value
    ga_eventValue: ''
  }, payload));
}
export default sendAnalytics;