import { SESSION_ID_KEY_NAME } from "@/hooks/useSessionId"

export function trackEvent(data: any) {
  if (
    typeof window !== "undefined" &&
    window.adobeDataLayer &&
    window.dataLayer
  ) {
    let sessionId = ""

    try {
      sessionId = sessionStorage.getItem(SESSION_ID_KEY_NAME) ?? ""
    } catch (e) {
      console.error("Error getting sessionId from sessionStorage", e)
    }

    data = { ...data, siteVersion: "new-web", sessionId }

    window.adobeDataLayer.push(data)
    window.dataLayer.push(data)
  }
}

export function trackClick(
  name: string,
  additionalParams?: Record<string, string>
) {
  trackEvent({
    event: "linkClick",
    cta: {
      ...additionalParams,
      name,
    },
  })
}
