import { TrackingPosition, TrackingSDKData } from "@/types/components/tracking"

export function trackClick(name: string) {
  if (typeof window !== "undefined" && window.adobeDataLayer) {
    window.adobeDataLayer.push({
      event: "linkClick",
      cta: {
        name,
      },
    })
  }
}

export function trackPageViewStart() {
  if (typeof window !== "undefined" && window.adobeDataLayer) {
    window.adobeDataLayer.push({
      event: "pageViewStart",
    })
  }
}

export function trackLoginClick(position: TrackingPosition) {
  if (typeof window !== "undefined" && window.adobeDataLayer) {
    const loginEvent = {
      event: "loginStart",
      login: {
        position,
        action: "login start",
        ctaName: "login",
      },
    }
    window.adobeDataLayer.push(loginEvent)
  }
}

export function createSDKPageObject(trackingData: TrackingSDKData) {
  const [lang, ...segments] = trackingData.pathName
    .split("/")
    .filter((seg: string) => seg)

  const joinedSegments = segments.join("|")

  const { host: domain } = window.location
  const page_obj = {
    pageType: trackingData.pageType,
    pageName: joinedSegments,
    pageId: trackingData.pageId,
    channel: trackingData.channel,
    siteSection: joinedSegments,
    domain,
    siteversion: "new-web",
    domainlanguage: trackingData.lang ? trackingData.lang : lang,
    createDate: trackingData.createdDate,
    publishDate: trackingData.publishedDate,
    // sessionid: "<unique identifier of session>", // base on what?
  }
  return page_obj
}
