/* LuckZoo Part 3B1 — Players display lock
 * Purpose: stop legacy zoo.js from alternating:
 * "Zoo Room 1 1/30" <-> "👤 30 Players"
 *
 * Additive only. Does not touch board geometry, betting, RNG, round timing,
 * winner animation, accounting, bots, deposits, or settlement.
 */
(()=>{
  'use strict';

  const FINAL_TEXT = '👤 30 Players';
  let target = null;
  let observer = null;

  function locate(){
    return document.querySelector(
      '#players, #playerCount, #roomPlayers, [data-role="players"], .players-count, .room-players, #room'
    );
  }

  function enforce(){
    const el = locate();
    if(!el) return;

    if(target !== el){
      if(observer) observer.disconnect();
      target = el;

      observer = new MutationObserver(()=>{
        if(target && target.textContent !== FINAL_TEXT){
          target.textContent = FINAL_TEXT;
        }
      });

      observer.observe(target,{
        childList:true,
        characterData:true,
        subtree:true
      });
    }

    if(target.textContent !== FINAL_TEXT){
      target.textContent = FINAL_TEXT;
    }
  }

  // Run after the old UI refresh has had a chance to write its room string.
  function start(){
    enforce();

    // The legacy game refresh can rewrite #room after every round.
    // This interval is only a fallback; MutationObserver normally corrects it instantly.
    setInterval(enforce,250);

    // Also handle DOM replacement/re-render.
    const pageObserver = new MutationObserver(enforce);
    pageObserver.observe(document.documentElement,{
      childList:true,
      subtree:true
    });
  }

  if(document.readyState === 'loading'){
    document.addEventListener('DOMContentLoaded', start, {once:true});
  }else{
    start();
  }
})();
