/**
 * share-sidebar.js
 * Social sharing sidebar with copy-to-clipboard
 */

export function initShareSidebar() {
  'use strict';

  const sidebar = document.getElementById('share-sidebar');
  const copyBtn = document.getElementById('share-copy');

  if (!sidebar) return;

  // Share button handlers
  const shareBtns = sidebar.querySelectorAll('.share-btn[data-platform]');
  shareBtns.forEach((btn) => {
    btn.addEventListener('click', () => {
      const platform = btn.getAttribute('data-platform');
      const url = sidebar.getAttribute('data-url');
      const title = sidebar.getAttribute('data-title');

      const shareUrls = {
        whatsapp: `https://wa.me/?text=${encodeURIComponent(title + ' ' + url)}`,
        telegram: `https://t.me/share/url?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
        linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(url)}`,
        twitter: `https://twitter.com/intent/tweet?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`,
        facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
        reddit: `https://reddit.com/submit?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}`,
        email: `mailto:?subject=${encodeURIComponent(title)}&body=${encodeURIComponent(url)}`,
      };

      if (shareUrls[platform]) {
        window.open(shareUrls[platform], '_blank');
      }
    });
  });

  // Copy to clipboard
  if (copyBtn) {
    copyBtn.addEventListener('click', () => {
      const url = sidebar.getAttribute('data-url');
      navigator.clipboard.writeText(url).then(() => {
        const originalText = copyBtn.innerHTML;
        copyBtn.innerHTML = '✓';
        setTimeout(() => {
          copyBtn.innerHTML = originalText;
        }, 2000);
      });
    });
  }
}
