(function(){
  'use strict';
  
  function init() {
    const checkbox = document.getElementById('foodord_show_product_card_images');
    if (!checkbox) return;
    
    // Add visual feedback when checkbox changes
    checkbox.addEventListener('change', function() {
      const label = this.closest('label');
      if (label) {
        // Toggle highlight class instead of inline styles
        label.classList.add('foodord-highlight');
        if (this.checked) {
          label.classList.add('foodord-highlight--positive');
          label.classList.remove('foodord-highlight--negative');
        } else {
          label.classList.add('foodord-highlight--negative');
          label.classList.remove('foodord-highlight--positive');
        }
        setTimeout(() => {
          label.classList.remove('foodord-highlight','foodord-highlight--positive','foodord-highlight--negative');
        }, 300);
      }
      
      // Update help text dynamically
      const helpText = this.closest('.wrc-control')?.querySelector('.wrc-help');
      if (helpText) {
        if (this.checked) {
          helpText.innerHTML = 'Product images will be displayed on cards. <strong>Remember to save changes.</strong>';
        } else {
          helpText.innerHTML = 'Product images will be hidden on cards. <strong>Remember to save changes.</strong>';
        }
      }
    });
  }
  
  // Auto hide only success notices after saving (after 5 seconds)
  function autoHideNotices() {
    const successNotices = document.querySelectorAll('.notice.notice-success.is-dismissible');
    if (successNotices.length > 0) {
      successNotices.forEach(function(notice) {
        setTimeout(function() {
          notice.classList.add('notice-fade-out');
          setTimeout(function() {
            notice.remove();
          }, 500);
        }, 5000);
      });
    }
  }
  
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function() {
      init();
      setTimeout(autoHideNotices, 1000);
    });
  } else {
    init();
    setTimeout(autoHideNotices, 1000);
  }
})();
