"use client"

import Link from "next/link"
import { type ComponentProps, type PropsWithChildren } from "react"

import { trackClick } from "@/utils/tracking"

import { variants } from "./variants"

import type { VariantProps } from "class-variance-authority"

export interface ButtonLinkProps
  extends PropsWithChildren,
    Omit<ComponentProps<typeof Link>, "color">,
    VariantProps<typeof variants> {
  trackingId?: string
  trackingParams?: Record<string, string>
}

export default function ButtonLink({
  variant,
  color,
  size,
  typography,
  wrapping,
  className,
  href,
  target,
  onClick = () => {},
  trackingId,
  trackingParams,
  ...props
}: ButtonLinkProps) {
  const classNames = variants({
    variant,
    color,
    size,
    wrapping,
    typography,
    className,
  })

  return (
    <Link
      className={classNames}
      href={href}
      target={target}
      onClick={(e) => {
        onClick(e)
        if (trackingId) {
          trackClick(trackingId, trackingParams)
        }
      }}
      {...props}
    />
  )
}
