"use client"

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

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

import type { PropsWithChildren } from "react"

import type { TrackingPosition } from "@/types/components/tracking"
import type { LinkProps } from "@/components/TempDesignSystem/Link/link"

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>
  )
}
