


window.AccueilJeu = (function() {

  
  
  
  
  function initToggleSection(carte, section, toutesLesSections, options) {
    options = options || {};
    var avec = options.avec || [];
    var ouvrir = function() {
      section.classList.toggle('visible');
      var ouvert = section.classList.contains('visible');
      avec.forEach(function(s) { s.classList.toggle('visible', ouvert); });
      toutesLesSections.forEach(function(s) {
        if (s !== section && avec.indexOf(s) < 0) s.classList.remove('visible');
      });
      if (section.classList.contains('visible')) {
        if (options.onOuvert) options.onOuvert();
        setTimeout(function() {
          section.scrollIntoView({ behavior: 'smooth', block: 'start' });
        }, 100);
      }
    };
    carte.addEventListener('click', ouvrir);
    carte.addEventListener('keydown', function(e) {
      if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); ouvrir(); }
    });
  }

  
  
  
  
  function initVisibiliteToggle(grilleId, zoneAfficherId, onChange) {
    var grille = document.getElementById(grilleId);
    if (!grille) return;
    grille.querySelectorAll('.visibilite-btn').forEach(function(btn) {
      btn.addEventListener('click', function() {
        grille.querySelectorAll('.visibilite-btn').forEach(function(b) { b.classList.remove('active'); });
        btn.classList.add('active');
        var valeur = btn.dataset.visibilite;
        if (zoneAfficherId) {
          var zone = document.getElementById(zoneAfficherId);
          if (zone) zone.style.display = valeur === 'publique' ? 'block' : 'none';
        }
        if (onChange) onChange(valeur);
      });
    });
  }

  
  function initGrilleDifficulte(grilleId, onChange) {
    var grille = document.getElementById(grilleId);
    if (!grille) return;
    grille.querySelectorAll('.difficulte-btn').forEach(function(btn) {
      btn.addEventListener('click', function() {
        grille.querySelectorAll('.difficulte-btn').forEach(function(b) { b.classList.remove('active'); });
        btn.classList.add('active');
        if (onChange) onChange(btn.dataset.difficulte);
      });
    });
  }

  
  function filtrerPseudo(el) {
    if (!el) return;
    el.addEventListener('input', function() {
      el.value = el.value.replace(/[^a-zA-Z0-9À-ɏ _-]/g, '').substring(0, 30);
    });
  }

  
  function attacherValidationCode(el) {
    if (!el) return;
    el.addEventListener('input', function() {
      el.value = el.value.toUpperCase().replace(/[^A-Z0-9]/g, '');
    });
  }

  
  function afficherErreur(el, msg, timeout) {
    if (!el) return;
    el.textContent = msg;
    setTimeout(function() { el.textContent = ''; }, timeout || 4000);
  }

  
  function echapperHtml(str) {
    var div = document.createElement('div');
    div.textContent = str == null ? '' : String(str);
    return div.innerHTML;
  }

  
  function prefillPseudoConnecte(elements, pseudo) {
    if (!pseudo) return;
    var liste = Array.isArray(elements) ? elements : [elements];
    liste.forEach(function(el) {
      if (!el) return;
      el.value = pseudo;
      el.disabled = true;
      el.style.opacity = '0.7';
    });
  }

  
  function attacherEnterKey(input, btn) {
    if (!input || !btn) return;
    input.addEventListener('keydown', function(e) {
      if (e.key === 'Enter') btn.click();
    });
  }

  
  function attacherPollingSection(section, fn, intervalMs) {
    setInterval(function() {
      if (section && section.classList.contains('visible')) fn();
    }, intervalMs || 5000);
  }

  return {
    initToggleSection: initToggleSection,
    initVisibiliteToggle: initVisibiliteToggle,
    initGrilleDifficulte: initGrilleDifficulte,
    filtrerPseudo: filtrerPseudo,
    attacherValidationCode: attacherValidationCode,
    afficherErreur: afficherErreur,
    echapperHtml: echapperHtml,
    prefillPseudoConnecte: prefillPseudoConnecte,
    attacherEnterKey: attacherEnterKey,
    attacherPollingSection: attacherPollingSection
  };
})();
