"use client";

import { useTranslations } from "next-intl";
import { Share2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";

export function ShareRecipeButton({ recipeId, visibility }: { recipeId: string; visibility: string }) {
  const t = useTranslations("recipe");

  async function handleShare() {
    const url = `${window.location.origin}/r/${recipeId}`;
    try {
      await navigator.clipboard.writeText(url);
    } catch {
      toast.error(t("shareCopyFailed"));
      return;
    }
    if (visibility === "public") {
      toast.success(t("shareLinkCopied"));
    } else {
      toast.info(t("shareNotPublicNotice"));
    }
  }

  return (
    <TooltipProvider>
      <Tooltip>
        <TooltipTrigger render={
          <Button variant="ghost" size="icon" onClick={() => void handleShare()}>
            <Share2 className="h-4 w-4" />
          </Button>
        } />
        <TooltipContent>{t("share")}</TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
}
