const _excluded = ["componentName", "componentPosition"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
export function getTrackingEnv() {
  var _window;
  const {
    hostname
  } = ((_window = window) === null || _window === void 0 ? void 0 : _window.location) || {};
  if (hostname !== null && hostname !== void 0 && hostname.includes("dev") || hostname !== null && hostname !== void 0 && hostname.includes("stag")) {
    return "stag";
  }
  if (hostname === "maps.trafimage.ch" || hostname === "trafimage-maps.sbb.ch") {
    return "prod";
  }
  return null;
}
export function getTrackingLaunchScriptSrc() {
  const env = getTrackingEnv();
  if (env === "stag") {
    return "https://assets.adobedtm.com/15ff638fdec4/97b4268d6157/launch-27b9efc6a00a-development.min.js";
  }
  if (env === "prod") {
    return "https://assets.adobedtm.com/15ff638fdec4/97b4268d6157/launch-6d0c92e1ddbe.min.js";
  }
  return null;
}
let prevUrl;
export const trackTopic = (activeTopic, language) => {
  const env = getTrackingEnv();
  const isIframe = window !== window.parent;
  if (env && activeTopic !== null && activeTopic !== void 0 && activeTopic.key && !activeTopic.noTracking) {
    var _window$location$ance;
    const parentLocation = isIframe ? ((_window$location$ance = window.location.ancestorOrigins) === null || _window$location$ance === void 0 ? void 0 : _window$location$ance[0]) || document.referrer : "";
    window.digitalDataLayer = window.digitalDataLayer || [];
    window.digitalDataLayer.push({
      pageInstanceID: env === "prod" ? "584988" : "584988",
      // For now the prod and stag IDs are the same
      page: {
        pageInfo: {
          pageName: activeTopic.key,
          destinationURL: window.location.href,
          destinationURI: window.location.pathname,
          referringURL: prevUrl || document.referrer,
          parentLocation,
          sysEnv: env === "prod" ? "production" : "integration",
          language
        },
        category: {
          primaryCategory: "maps.trafimage.ch"
        }
      }
    });
    window.digitalDataLayer.push({
      event: {
        eventInfo: {
          eventName: "page data layer ready"
        }
      }
    });
    prevUrl = window.location.href;
  }
};
export function trackEvent(_ref, topic) {
  let {
      componentName,
      componentPosition = 1
    } = _ref,
    eventInfo = _objectWithoutProperties(_ref, _excluded);
  if (!getTrackingEnv() || topic !== null && topic !== void 0 && topic.noTracking) {
    return;
  }
  window.digitalDataLayer = window.digitalDataLayer || [];
  window.digitalDataLayer.push({
    event: {
      eventInfo: _objectSpread({
        componentPath: [{
          name: "Component: ".concat(componentName),
          position: componentPosition
        }],
        eventName: "click",
        destination: ""
      }, eventInfo),
      category: {
        primaryCategory: eventInfo.eventType === "download" ? "download" : "generic navigation"
      }
    }
  });
}
export default {
  getTrackingEnv,
  getTrackingLaunchScriptSrc,
  trackEvent
};
//# sourceMappingURL=trackingUtils.js.map