/*activa el cache para las paginas del menu*/
document.addEventListener('DOMContentLoaded', function() {
  if (window.innerWidth > 768) return;

  var items = document.querySelectorAll('.bnav-item');
  var prefetched = new Set();

  function normPath(url) {
    try {
      var u = new URL(url, window.location.origin);
      var p = u.pathname;
      if (p.length > 1 && p.charAt(p.length - 1) === '/') p = p.slice(0, -1);
      return p;
    } catch (e) { return null; }
  }

  var currentPath = normPath(window.location.href);

  function prefetch(url) {
    if (!url || prefetched.has(url)) return;
    prefetched.add(url);
    var link = document.createElement('link');
    link.rel = 'prefetch';
    link.href = url;
    document.head.appendChild(link);
  }

  items.forEach(function(item) {
    var href = item.getAttribute('href');
    var itemPath = href ? normPath(href) : null;

    if (itemPath && itemPath === currentPath) {
      item.classList.add('is-active');
    }

    item.addEventListener('touchstart', function() {
      prefetch(href);
    }, { passive: true });
  });

  if ('requestIdleCallback' in window) {
    requestIdleCallback(function() {
      items.forEach(function(item) {
        prefetch(item.getAttribute('href'));
      });
    });
  }

  document.body.classList.add('has-bottom-nav');
});