import { trackEvent } from "./base"

import type { TrackingSDKData } from "@/types/components/tracking"

function convertSlashToPipe(url: string) {
  const formattedUrl = url.startsWith("/") ? url.slice(1) : url
  return formattedUrl.replaceAll("/", "|")
}

export function trackPageViewStart() {
  trackEvent({
    event: "pageViewStart",
  })
}

export function trackPageView(data: any) {
  trackEvent(data)
}

export function createSDKPageObject(
  trackingData: TrackingSDKData
): TrackingSDKData {
  let pageName = convertSlashToPipe(trackingData.pageName)
  let siteSections = convertSlashToPipe(trackingData.siteSections)

  if (trackingData.pathName.indexOf("/webview/") > -1) {
    pageName = "webview|" + pageName
    siteSections = "webview|" + siteSections
  }

  return {
    ...trackingData,
    domain: typeof window !== "undefined" ? window.location.host : "",
    pageName: pageName,
    siteSections: siteSections,
  }
}
