"use client"

import { usePathname } from "next/navigation"

import { useTrackHardNavigation } from "@scandic-hotels/common/tracking/useTrackHardNavigation"
import { useTrackSoftNavigation } from "@scandic-hotels/common/tracking/useTrackSoftNavigation"
import { trpc } from "@scandic-hotels/trpc/client"

import useLang from "@/hooks/useLang"

import type {
  TrackingSDKAncillaries,
  TrackingSDKHotelInfo,
  TrackingSDKPageData,
  TrackingSDKPaymentInfo,
} from "@scandic-hotels/common/tracking/types"

export default function TrackingSDK({
  pageData,
  hotelInfo,
  paymentInfo,
  ancillaries,
}: {
  pageData: TrackingSDKPageData
  hotelInfo?: TrackingSDKHotelInfo
  paymentInfo?: TrackingSDKPaymentInfo
  ancillaries?: TrackingSDKAncillaries
}) {
  const lang = useLang()
  const pathName = usePathname()
  const { data, isError } = trpc.user.userTrackingInfo.useQuery({
    lang,
  })

  const userData = isError ? ({ loginStatus: "Error" } as const) : data

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

  return null
}
