(()=>{
  'use strict';
  const norm=v=>String(v||'').toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g,'').trim();
  const role=()=>norm((window.currentUser&&window.currentUser.role)||(window.currentProfile&&window.currentProfile.role)||localStorage.getItem('seniorcare-role'));
  const isAssistant=()=>['assistant','asistente','caregiver'].includes(role());
  const isOperational=()=>['cook','cocina','kitchen','cleaning','limpieza','miscelanea','miscellaneous','maintenance','mantenimiento','security','seguridad','guard','guarda','guarda de seguridad'].includes(role());
  const blockedAssistant=['medicamento','curacion','nota de enfermeria','nota medica','egreso','epicrisis','enviar al familiar','familiares'];
  const blockedOperational=['residente','paciente','medicamento','curacion','nota de enfermeria','nota medica','egreso','epicrisis','familiar','signos vitales','plan de cuidados','nutricion','citas','traslados','alertas generales'];
  const text=n=>norm(n?.getAttribute?.('aria-label')||n?.dataset?.go||n?.dataset?.residentGo||n?.textContent||'');
  const shouldBlock=n=>{
    const t=text(n);
    if(isAssistant()&&blockedAssistant.some(x=>t.includes(x))) return true;
    if(isOperational()&&blockedOperational.some(x=>t.includes(x))) return true;
    return false;
  };
  function clean(){
    if(!isAssistant()&&!isOperational()) return;
    document.querySelectorAll('button,a,[data-go],[data-resident-go],.module-card,.quick-card,.admin-stat').forEach(n=>{
      if(shouldBlock(n)){n.hidden=true;n.setAttribute('aria-hidden','true');n.tabIndex=-1;}
    });
    if(isOperational()){
      const navs=[...document.querySelectorAll('.operational-nav,.op-bottom-nav')];
      navs.slice(0,-1).forEach(n=>n.remove());
      const attendance=[...document.querySelectorAll('.op-self-card')];
      attendance.slice(0,-1).forEach(n=>n.remove());
      document.querySelectorAll('[data-go="alerts"],[data-go="residents"],[data-go="resident-list"],#alertsScreen,#residentScreen').forEach(n=>{n.hidden=true;});
    }
  }
  document.addEventListener('click',e=>{
    const target=e.target.closest('button,a,[data-go],[data-resident-go]');
    if(target&&shouldBlock(target)){
      e.preventDefault();e.stopImmediatePropagation();
      if(window.showToast) window.showToast('Esta función no está habilitada para este perfil.');
    }
  },true);
  const obs=new MutationObserver(()=>queueMicrotask(clean));
  document.addEventListener('DOMContentLoaded',()=>{clean();obs.observe(document.body,{childList:true,subtree:true});});
  window.BHSC_COMMERCIAL_BUILD='4.0.0';
  window.BHSC_ROLE_MATRIX={supervisor:'global',administrator:'organizacion',nurse:'clinico',assistant:'cuidados_asignados',cook:'operativo',cleaning:'operativo',maintenance:'operativo',security:'operativo'};
})();
