'use client';

import React, { useState, useEffect } from 'react';
import Share from '@theme/views/share';
import { useLocalization } from '@akinon/next/hooks';

interface ProductShareProps {
  productName: string;
  className?: string;
}

export const ProductShare: React.FC<ProductShareProps> = ({
  productName,
  className
}) => {
  const { t } = useLocalization();
  const [currentUrl, setCurrentUrl] = useState<string | null>(null);

  useEffect(() => {
    setCurrentUrl(window.location.href);
  }, []);

  if (!currentUrl) {
    return null;
  }

  const shareItems = [
    {
      href: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(
        currentUrl
      )}`,
      iconName: 'facebook' as const,
      iconSize: 22
    },
    {
      href: `https://twitter.com/intent/tweet?text=${encodeURIComponent(
        currentUrl
      )}`,
      iconName: 'twitter' as const,
      iconSize: 22
    },
    {
      href: `https://api.whatsapp.com/send?text=${productName}%20${encodeURIComponent(
        currentUrl
      )}`,
      iconName: 'whatsapp' as const,
      iconSize: 22
    }
  ];

  return (
    <Share
      className={className}
      buttonText={t('product.share')}
      items={shareItems}
    />
  );
};