import React from "react";
import { toast } from "react-toastify";
import { FaLink } from "react-icons/fa";

type Props = { url: string; setUrl: any };

export default function ButtonShared({ url, setUrl }: Props) {
  const handleShare = async () => {
    if (navigator.share) {
      try {
        await navigator.share({
          title: "Sharing Title",
          text: "Sharing Text",
          url: url,
        });
        toast.success("Shared successfully");
      } catch (error: any) {
        toast.error("An error occurred while sharing:", error);
      }
    } else {
      toast.error("Web Share API is not supported in this browser");
      // Alternative sharing
      const shareUrl = `mailto:?subject=Sharing Title&body=Sharing text: ${url}`;
      setUrl(shareUrl);
    }
  };
  return (
    <FaLink
      onClick={handleShare}
      size={15}
      className="mr-3 cursor-pointer  text-primary bg-greysecondry rounded-full w-10 h-10 p-2"
      // round
    />
  );
}
