"use client"

import { usePathname } from "next/navigation"

import { trpc } from "@scandic-hotels/trpc/client"

import useLang from "./hooks/useLang"
import { useTrackHardNavigation } from "./useTrackHardNavigation"
import { useTrackSoftNavigation } from "./useTrackSoftNavigation"

import type {
  TrackingSDKAncillaries,
  TrackingSDKHotelInfo,
  TrackingSDKPageData,
  TrackingSDKPaymentInfo,
  TrackingSDKUserData,
} from "./types"

export function TrackingSDK({
  pageData,
  hotelInfo,
  paymentInfo,
  ancillaries,
}: {
  pageData: TrackingSDKPageData
  hotelInfo?: TrackingSDKHotelInfo
  paymentInfo?: TrackingSDKPaymentInfo
  ancillaries?: TrackingSDKAncillaries
}) {
  const pathName = usePathname()
  const lang = useLang()

  const { data, isError } = trpc.user.userTrackingInfo.useQuery({
    lang,
  })

  const userData: TrackingSDKUserData =
    !data || isError
      ? ({ loginStatus: "Error" } as const)
      : { ...data, memberType: "scandic-friends" }

  useTrackHardNavigation({
    pageData,
    hotelInfo,
    paymentInfo,
    ancillaries,
    userData,
    pathName,
  })
  useTrackSoftNavigation({
    pageData,
    hotelInfo,
    paymentInfo,
    ancillaries,
    userData,
    pathName,
  })

  return null
}
