"use client";

import NextLink from "next/link";
import { usePathname } from "next/navigation";
import type { ComponentProps } from "react";
import { withOriginPage } from "@/lib/cta";

/**
 * next/link that adds ?from=<current page> to links opening the contact or support form, so the lead records the
 * page of the click (ctx_origin_page, HUB-753). Every other link renders exactly as next/link would.
 */
export default function Link({ href, ...rest }: ComponentProps<typeof NextLink>) {
  const pathname = usePathname();
  const target = typeof href === "string" ? withOriginPage(href, pathname) : href;
  return <NextLink href={target} {...rest} />;
}
