(function(){
  document.addEventListener('DOMContentLoaded', () => {
    const customUrl = document.body.dataset.whatsappUrl;
    const adminPhone = document.body.dataset.adminPhone;
    const iconUrl = document.body.dataset.whatsappIconUrl;
    let finalUrl = '';

    // Tentukan URL yang akan digunakan
    if (customUrl) {
        // Jika URL khas wujud, guna yang itu
        finalUrl = customUrl;
    } else if (adminPhone) {
        // Jika tidak, bina pautan wa.me menggunakan nombor admin
        const defaultMessage = "Salam, saya berminat dengan produk Vanilla Blue.";
        finalUrl = `https://wa.me/${adminPhone}?text=${encodeURIComponent(defaultMessage)}`;
    }

    // Hanya cipta butang jika ada URL untuk digunakan
    if (finalUrl) {
      const bubbleLink = document.createElement('a');
      bubbleLink.id = 'chatbot-bubble';
      bubbleLink.href = finalUrl;
      bubbleLink.target = '_blank';
      bubbleLink.style.position = 'fixed';
      bubbleLink.style.right = '24px';
      bubbleLink.style.bottom = '24px';
      bubbleLink.style.zIndex = '60';
      bubbleLink.style.textDecoration = 'none';

      if (iconUrl) {
        // Jika ada ikon, guna gaya ikon
        bubbleLink.style.backgroundImage = `url(${iconUrl})`;
        bubbleLink.style.backgroundSize = 'contain';
        bubbleLink.style.backgroundPosition = 'center';
        bubbleLink.style.backgroundRepeat = 'no-repeat';
        bubbleLink.style.width = '60px';
        bubbleLink.style.height = '60px';
        bubbleLink.style.backgroundColor = 'transparent';
        bubbleLink.style.boxShadow = 'none';
      } else {
        // Jika tiada ikon, guna gaya teks
        bubbleLink.textContent = 'Chat di WhatsApp';
        bubbleLink.style.backgroundColor = '#25d366';
        bubbleLink.style.color = '#fff';
        bubbleLink.style.borderRadius = '999px';
        bubbleLink.style.padding = '12px 16px';
        bubbleLink.style.fontWeight = '800';
        bubbleLink.style.boxShadow = '0 10px 20px rgba(0,0,0,0.15)';
      }

      document.body.appendChild(bubbleLink);
    }
  });
})();