/* Bernard Health SeniorCare Enterprise V4.0.2
   Privacidad operativa, navegación y protección responsive */
(()=>{
  'use strict';
  const OPS=['cook','cleaning','maintenance','security'];
  const role=()=>String(window.currentUser?.role||'').toLowerCase();
  const isOperational=()=>OPS.includes(role());
  const allowed=new Set(['home','operationalTasks','operationalIncidents','operationalAlerts','nutrition']);
  function protectOperationalUi(){
    if(!isOperational())return;
    const alertButton=document.querySelector('.topbar .alerts');
    if(alertButton){
      alertButton.setAttribute('aria-label','Avisos de tareas');
      alertButton.onclick=e=>{e.preventDefault();e.stopImmediatePropagation();window.go?.('operationalAlerts',{scope:'global'});};
    }
    document.querySelectorAll('[data-go="alerts"],[data-nav="alerts"],[data-go="residents"],[data-nav="residents"]').forEach(n=>n.remove());
    if(window.activeScreen&&!allowed.has(String(window.activeScreen)))window.go?.('home',{scope:'global'});
  }
  document.addEventListener('click',e=>{
    if(!isOperational())return;
    const target=e.target.closest?.('[data-go],[data-nav],.alerts');
    if(!target)return;
    const screen=target.classList.contains('alerts')?'operationalAlerts':String(target.dataset.go||target.dataset.nav||'');
    if(screen==='alerts'||screen==='residents'||screen==='inactive'||screen==='resident'){
      e.preventDefault();e.stopImmediatePropagation();window.go?.('operationalAlerts',{scope:'global'});
    }
  },true);
  const observer=new MutationObserver(()=>protectOperationalUi());
  document.addEventListener('DOMContentLoaded',()=>{observer.observe(document.body,{subtree:true,childList:true});protectOperationalUi();});
  window.addEventListener('seniorcare:user-ready',()=>setTimeout(protectOperationalUi,100));
  window.addEventListener('resize',()=>document.documentElement.style.setProperty('--bhsc-vw',`${document.documentElement.clientWidth}px`));
  console.info('Operational privacy/responsive V4.0.2 listo');
})();
