"use client";
import { useState } from "react";
export function ArticleActions({ url, title }: { url: string; title: string }) {
  const [message, setMessage] = useState("");
  const encoded = encodeURIComponent(url),
    text = encodeURIComponent(title);
  return (
    <div className="article-actions" aria-label="Share this article">
      <span>Share this article</span>
      <div>
        <a
          href={`https://www.facebook.com/sharer/sharer.php?u=${encoded}`}
          target="_blank"
          rel="noopener noreferrer"
        >
          Facebook
        </a>
        <a
          href={`https://twitter.com/intent/tweet?url=${encoded}&text=${text}`}
          target="_blank"
          rel="noopener noreferrer"
        >
          X
        </a>
        <a
          href={`https://www.linkedin.com/sharing/share-offsite/?url=${encoded}`}
          target="_blank"
          rel="noopener noreferrer"
        >
          LinkedIn
        </a>
        <button
          type="button"
          onClick={async () => {
            try {
              await navigator.clipboard.writeText(url);
              setMessage("Article link copied.");
            } catch {
              setMessage("Copy this link: " + url);
            }
          }}
        >
          Copy link
        </button>
        <button type="button" onClick={() => window.print()}>
          Print
        </button>
      </div>
      {message && <p role="status">{message}</p>}
    </div>
  );
}
