"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";
import type { ComponentProps } from "react";

/**
 * A link that may point at a section of the current page (`#goals`, or `/ai-search#p-searchgpt-api` while on
 * /ai-search). Those stay plain anchors: the browser scrolls to the section on every click, while Next's <Link>
 * does nothing when you are already on that hash.
 */
export default function SectionLink({ href, ...props }: ComponentProps<typeof Link> & { href: string }) {
  const pathname = usePathname().replace(/\/$/, "") || "/";
  const [path, hash] = href.split("#");
  const samePage = hash !== undefined && (path === "" || path === pathname);
  return samePage ? <a href={href} {...props} /> : <Link href={href} {...props} />;
}
