"use client";

import { useState } from "react";

function ShareBtn({
  title,
  onClick,
  href,
  active,
  children,
}: {
  title: string;
  onClick?: () => void;
  href?: string;
  active?: boolean;
  children: React.ReactNode;
}) {
  if (href) {
    return (
      <a
        href={href}
        title={title}
        aria-label={title}
        target="_blank"
        rel="noopener noreferrer"
        className={`b-share-btn${active ? " active" : ""}`}
      >
        {children}
      </a>
    );
  }
  return (
    <button
      title={title}
      onClick={onClick}
      className={`b-share-btn${active ? " active" : ""}`}
      aria-label={title}
    >
      {children}
    </button>
  );
}

function useShare() {
  const [copied, setCopied] = useState(false);
  const getUrl = () => typeof window !== "undefined" ? encodeURIComponent(location.href) : "";
  const getTitle = () => typeof window !== "undefined" ? encodeURIComponent(document.title) : "";
  const copy = () => {
    try { navigator.clipboard.writeText(location.href); } catch {}
    setCopied(true);
    setTimeout(() => setCopied(false), 1600);
  };
  return { copied, copy, getUrl, getTitle };
}

export function BlogShareRail() {
  const { copied, copy, getUrl, getTitle } = useShare();

  return (
    <div className="b-share-rail" aria-label="Share options">
      <span className="b-share-label">Share</span>

      <ShareBtn title="Copy link" onClick={copy} active={copied}>
        {copied ? (
          <svg width="17" height="17" viewBox="0 0 24 24" fill="none">
            <path
              d="m5 13 4 4L19 7"
              stroke="currentColor"
              strokeWidth="2.2"
              strokeLinecap="round"
              strokeLinejoin="round"
            />
          </svg>
        ) : (
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
            <path
              d="M10 14a3.5 3.5 0 0 0 5 0l3-3a3.5 3.5 0 1 0-5-5l-1 1"
              stroke="currentColor"
              strokeWidth="2"
              strokeLinecap="round"
            />
            <path
              d="M14 10a3.5 3.5 0 0 0-5 0l-3 3a3.5 3.5 0 1 0 5 5l1-1"
              stroke="currentColor"
              strokeWidth="2"
              strokeLinecap="round"
            />
          </svg>
        )}
      </ShareBtn>

      <ShareBtn
        title="Share on X"
        href={`https://twitter.com/intent/tweet?url=${getUrl()}&text=${getTitle()}`}
      >
        <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
          <path d="M18.244 2H21.5l-7.5 8.57L23 22h-6.59l-5.16-6.74L5.34 22H2.08l8.02-9.17L1.5 2h6.76l4.66 6.16L18.244 2Zm-1.16 18h1.83L7.01 3.9H5.05l12.034 16.1Z" />
        </svg>
      </ShareBtn>

      <ShareBtn
        title="Share on LinkedIn"
        href={`https://www.linkedin.com/sharing/share-offsite/?url=${getUrl()}`}
      >
        <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
          <path d="M4.98 3.5a2.5 2.5 0 1 1 0 5 2.5 2.5 0 0 1 0-5ZM3 9h4v12H3V9Zm6 0h3.8v1.7h.05c.53-1 1.83-2.05 3.77-2.05C20.6 8.65 22 10.3 22 13.6V21h-4v-6.5c0-1.55-.03-3.55-2.16-3.55-2.17 0-2.5 1.7-2.5 3.44V21H9V9Z" />
        </svg>
      </ShareBtn>

      <ShareBtn
        title="Share on Facebook"
        href={`https://www.facebook.com/sharer/sharer.php?u=${getUrl()}`}
      >
        <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
          <path d="M24 12.073C24 5.405 18.627 0 12 0S0 5.405 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.41c0-3.025 1.792-4.697 4.533-4.697 1.312 0 2.686.235 2.686.235v2.97h-1.513c-1.491 0-1.956.93-1.956 1.886v2.268h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073Z" />
        </svg>
      </ShareBtn>
    </div>
  );
}

export function BlogShareRailMobile() {
  const { copied, copy, getUrl, getTitle } = useShare();

  return (
    <div className="b-share-mobile" aria-label="Share options">
      <span className="b-share-mobile-label">Share</span>
      <div className="b-share-mobile-btns">
        <ShareBtn title="Copy link" onClick={copy} active={copied}>
          {copied ? (
            <svg width="17" height="17" viewBox="0 0 24 24" fill="none">
              <path d="m5 13 4 4L19 7" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
          ) : (
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none">
              <path d="M10 14a3.5 3.5 0 0 0 5 0l3-3a3.5 3.5 0 1 0-5-5l-1 1" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
              <path d="M14 10a3.5 3.5 0 0 0-5 0l-3 3a3.5 3.5 0 1 0 5 5l1-1" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
            </svg>
          )}
        </ShareBtn>

        <ShareBtn title="Share on X" href={`https://twitter.com/intent/tweet?url=${getUrl()}&text=${getTitle()}`}>
          <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
            <path d="M18.244 2H21.5l-7.5 8.57L23 22h-6.59l-5.16-6.74L5.34 22H2.08l8.02-9.17L1.5 2h6.76l4.66 6.16L18.244 2Zm-1.16 18h1.83L7.01 3.9H5.05l12.034 16.1Z" />
          </svg>
        </ShareBtn>

        <ShareBtn title="Share on LinkedIn" href={`https://www.linkedin.com/sharing/share-offsite/?url=${getUrl()}`}>
          <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
            <path d="M4.98 3.5a2.5 2.5 0 1 1 0 5 2.5 2.5 0 0 1 0-5ZM3 9h4v12H3V9Zm6 0h3.8v1.7h.05c.53-1 1.83-2.05 3.77-2.05C20.6 8.65 22 10.3 22 13.6V21h-4v-6.5c0-1.55-.03-3.55-2.16-3.55-2.17 0-2.5 1.7-2.5 3.44V21H9V9Z" />
          </svg>
        </ShareBtn>

        <ShareBtn title="Share on Facebook" href={`https://www.facebook.com/sharer/sharer.php?u=${getUrl()}`}>
          <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor">
            <path d="M24 12.073C24 5.405 18.627 0 12 0S0 5.405 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.41c0-3.025 1.792-4.697 4.533-4.697 1.312 0 2.686.235 2.686.235v2.97h-1.513c-1.491 0-1.956.93-1.956 1.886v2.268h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073Z" />
          </svg>
        </ShareBtn>
      </div>
    </div>
  );
}
