"use client"

import { PropsWithChildren } from "react"

import { login } from "@/constants/routes/handleAuth"

import Link from "@/components/TempDesignSystem/Link"
import { LinkProps } from "@/components/TempDesignSystem/Link/link"
import useLang from "@/hooks/useLang"
import { useLazyPathname } from "@/hooks/useLazyPathname"
import { trackLoginClick } from "@/utils/tracking"

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

export default function LoginButton({
  position,
  trackingId,
  children,
  ...props
}: PropsWithChildren<
  {
    trackingId: string
    position: TrackingPosition
  } & Omit<LinkProps, "href">
>) {
  const lang = useLang()
  const pathName = useLazyPathname({ includeSearchParams: true })

  const href = pathName
    ? `${login[lang]}?redirectTo=${encodeURIComponent(pathName)}`
    : login[lang]

  return (
    <Link
      id={trackingId}
      href={href}
      prefetch={false}
      onClick={() => trackLoginClick(position)}
      {...props}
    >
      {children}
    </Link>
  )
}
