/* Header-specific JavaScript - Loaded only when needed */
(function() {
  'use strict';
  
  // Use Intersection Observer for lazy initialization
  if ('IntersectionObserver' in window) {
    const header = document.querySelector('.header');
    
    if (!header) return;
    
    const headerObserver = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          initializeHeader();
          headerObserver.unobserve(entry.target);
        }
      });
    });
    
    headerObserver.observe(header);
  } else {
    // Fallback for browsers without Intersection Observer
    document.addEventListener('DOMContentLoaded', initializeHeader);
  }
  
  function initializeHeader() {
    const nav = document.querySelector('.header__navigation');
    const langSwitcher = document.querySelector('.header__language-switcher');
    const search = document.querySelector('.header__search');
    const allToggles = document.querySelectorAll('.header--toggle');
    const navToggle = document.querySelector('.header__navigation--toggle');
    const langToggle = document.querySelector('.header__language-switcher--toggle');
    const searchToggle = document.querySelector('.header__search--toggle');
    const closeToggle = document.querySelector('.header__close--toggle');
    const allElements = document.querySelectorAll('.header--element, .header--toggle');
    
    // Optimized toggle functions using event delegation
    function toggleNav() {
      requestAnimationFrame(() => {
        allToggles.forEach(toggle => toggle.classList.toggle('hide'));
        nav.classList.toggle('open');
        navToggle.classList.toggle('open');
        closeToggle.classList.toggle('show');
      });
    }
    
    function toggleLang() {
      requestAnimationFrame(() => {
        allToggles.forEach(toggle => toggle.classList.toggle('hide'));
        langSwitcher.classList.toggle('open');
        langToggle.classList.toggle('open');
        closeToggle.classList.toggle('show');
      });
    }
    
    function toggleSearch() {
      requestAnimationFrame(() => {
        allToggles.forEach(toggle => toggle.classList.toggle('hide'));
        search.classList.toggle('open');
        searchToggle.classList.toggle('open');
        closeToggle.classList.toggle('show');
      });
    }
    
    function closeAll() {
      requestAnimationFrame(() => {
        allElements.forEach(element => {
          element.classList.remove('hide', 'open');
        });
        closeToggle.classList.remove('show');
      });
    }
    
    // Add event listeners with passive option for better scrolling performance
    if (langSwitcher && langToggle) {
      langToggle.addEventListener('click', toggleLang, { passive: true });
    }
    
    if (navToggle) {
      navToggle.addEventListener('click', toggleNav, { passive: true });
    }
    
    if (searchToggle) {
      searchToggle.addEventListener('click', toggleSearch, { passive: true });
    }
    
    if (closeToggle) {
      closeToggle.addEventListener('click', closeAll, { passive: true });
    }
  }
})();