"use client";

import { cn } from "@/lib/utils";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { ComponentProps, forwardRef } from "react";

type AnchorProps = ComponentProps<typeof Link> & {
  absolute?: boolean;
  activeClassName?: string;
  disabled?: boolean;
};

const Anchor = forwardRef<HTMLAnchorElement, AnchorProps>(
  ({ absolute, className = "", activeClassName = "", disabled, children, ...props }, ref) => {
    const path = usePathname();
    let isMatch = absolute
      ? props.href.toString().split("/")[1] == path.split("/")[1]
      : path === props.href;

    if (props.href.toString().includes("http")) isMatch = false;

    if (disabled)
      return (
        <div className={cn(className, "cursor-not-allowed")}>{children}</div>
      );

    return (
      <Link ref={ref} className={cn(className, isMatch && activeClassName)} {...props}>
        {children}
      </Link>
    );
  }
);
// ✅ Tambahkan display name agar tidak error saat build
Anchor.displayName = "Anchor";

export default Anchor;
