// assets/js/ua_modern.js
(function(){
  function qs(sel, root){ return (root||document).querySelector(sel); }
  function qsa(sel, root){ return Array.from((root||document).querySelectorAll(sel)); }

  // Dropdowns (mega menu)
  function closeAllDropdowns(except){
    qsa('.ua-dd.is-open').forEach(function(dd){
      if (except && dd === except) return;
      dd.classList.remove('is-open');
      var btn = dd.querySelector('.ua-pill');
      if (btn) btn.setAttribute('aria-expanded','false');
    });
  }

  document.addEventListener('click', function(e){
    var ddBtn = e.target.closest && e.target.closest('.ua-dd > .ua-pill');
    if (ddBtn){
      var dd = ddBtn.parentElement;
      var open = dd.classList.contains('is-open');
      closeAllDropdowns(dd);
      dd.classList.toggle('is-open', !open);
      ddBtn.setAttribute('aria-expanded', (!open).toString());
      e.preventDefault();
      return;
    }
    // Click inside menu: allow navigation
    if (e.target.closest && e.target.closest('.ua-menu')) return;

    closeAllDropdowns();
  });

  // Mobile sidebar open/close (desktop is auto-expand on hover)
  function toggleMobileSidebar(){
    if (!document.body.classList.contains('ua-authed')) return;
    if (window.matchMedia && window.matchMedia('(max-width: 992px)').matches){
      document.body.classList.toggle('ua-sidebar-open');
    }
  }
  document.addEventListener('click', function(e){
    var t = e.target.closest && e.target.closest('[data-ua-mobile-menu]');
    if (!t) return;
    e.preventDefault();
    toggleMobileSidebar();
  });

  // Desktop: keep sidebar compact and fixed (no auto-expand / auto-hover open)
  function ensureMini(){
    if (!document.body.classList.contains('ua-authed')) return;
    if (window.matchMedia && window.matchMedia('(max-width: 992px)').matches) return;
    document.body.classList.remove('ua-sidebar-mini');
    return;

    // When sidebar collapses, auto-close any open accordion sections
    try{
      qsa('.ua-sb-sec.is-open').forEach(function(sec){
        sec.classList.remove('is-open');
        var btn = sec.querySelector('.ua-sb-sec-btn');
        if (btn) btn.setAttribute('aria-expanded','false');
        var k = sec.getAttribute('data-ua-acc') || '';
        try{ localStorage.setItem('ua_acc_'+k,'0'); }catch(_){ }
      });
    }catch(_){ }
  }
  document.addEventListener('DOMContentLoaded', function(){
    });

  // Note: accordion sections are opened by click only (see handlers below).

  

  // Smart sidebar: tooltips for mini mode + mobile backdrop
  document.addEventListener('DOMContentLoaded', function(){
    qsa('.ua-sb-link, .ua-sb-sec-btn').forEach(function(a){
      var txtEl = a.querySelector('span');
      var txt = (txtEl ? txtEl.textContent : '').trim();
      if (txt) a.setAttribute('data-ua-tip', txt);
      if (txt && !a.getAttribute('aria-label')) a.setAttribute('aria-label', txt);
    });
  });

  // Backdrop closes mobile sidebar
  document.addEventListener('click', function(e){
    var bd = e.target.closest && e.target.closest('.ua-sb-backdrop');
    if (!bd) return;
    document.body.classList.remove('ua-sidebar-open');
  });

// Highlight active sidebar link
  document.addEventListener('DOMContentLoaded', function(){
    var path = (location.pathname || '').split('/').pop();
    if (!path) return;
    qsa('.ua-sb-link, .ua-sb-sublink').forEach(function(a){
      var href = (a.getAttribute('href')||'').split('/').pop();
      if (href && href === path){
        a.classList.add('is-active');
      }
    });
  });

  // Login modal open/close
  function openLogin(){
    var m = qs('#uaLoginModal');
    if (!m) return;
    m.classList.add('is-open');
    var inp = m.querySelector('input[name=\"user\"]');
    if (inp) setTimeout(function(){ inp.focus(); }, 50);
  }
  function closeLogin(){
    var m = qs('#uaLoginModal');
    if (!m) return;
    m.classList.remove('is-open');
    // remove ?login=1 from url without reload
    try{
      var url = new URL(window.location.href);
      url.searchParams.delete('login');
      history.replaceState({}, '', url.toString());
    }catch(_){}
  }
  document.addEventListener('click', function(e){
    if (e.target.closest && e.target.closest('[data-ua-open-login]')){
      e.preventDefault();
      openLogin();
    }
    if (e.target.closest && e.target.closest('[data-ua-close-login]')){
      e.preventDefault();
      closeLogin();
    }
  });
  document.addEventListener('keydown', function(e){
    if (e.key === 'Escape'){
      closeLogin();
      document.body.classList.remove('ua-sidebar-open');
    }
  });

  // Top search: search by username (redirect to subscribers.php?q=...)
  // Top search form submit fallback
  document.addEventListener('submit', function(e){
    var f = e.target && e.target.matches && e.target.matches('[data-ua-search-form]');
    if (!f) return;
    var inp = f.querySelector('[data-ua-search]');
    var q = (inp && inp.value ? inp.value : '').trim();
    if (!q){ e.preventDefault(); return; }
    // allow normal GET submit
  });

  document.addEventListener('keydown', function(e){
    var inp = e.target && e.target.matches && e.target.matches('[data-ua-search]');
    if (!inp) return;
    if (e.key !== 'Enter') return;
    var q = (inp.value||'').trim();
    if (!q) return;
    e.preventDefault();
    window.location.href = 'subscribers.php?q=' + encodeURIComponent(q);
  });

  // Sidebar accordion (sections)
  function setAccOpen(secEl, open){
    if (!secEl) return;
    secEl.classList.toggle('is-open', !!open);
    var btn = secEl.querySelector('.ua-sb-sec-btn');
    if (btn) btn.setAttribute('aria-expanded', (!!open).toString());
  }
  document.addEventListener('click', function(e){
    var btn = e.target.closest && e.target.closest('.ua-sb-sec-btn');
    if (!btn) return;
    var sec = btn.closest('.ua-sb-sec');
    if (!sec) return;
    var open = sec.classList.contains('is-open');
    setAccOpen(sec, !open);
    try{ localStorage.setItem('ua_acc_'+(sec.getAttribute('data-ua-acc')||''), (!open)?'1':'0'); }catch(_){ }
  });
  document.addEventListener('DOMContentLoaded', function(){
    qsa('.ua-sb-sec').forEach(function(sec){
      var k = sec.getAttribute('data-ua-acc') || '';
      var open = false;
      try{ open = localStorage.getItem('ua_acc_'+k) === '1'; }catch(_){ }
      // auto-open if contains active link
      if (sec.querySelector('.ua-sb-sublink.is-active')) open = true;
      setAccOpen(sec, open);
    });
  });

  
// Drag resize removed (hover-expand only)
})();
