/* Associação Clube — PWA Client v33.3.5 */
(function(){
'use strict';
var deferredPrompt=null;
var installButton=null;
var toastTimer=null;
function ready(fn){
if(document.readyState!=='loading')fn();
else document.addEventListener('DOMContentLoaded',fn);
}
function toast(message,type){
var el=document.getElementById('v33253PwaToast');
if(!el){
el=document.createElement('div');
el.id='v33253PwaToast';
el.className='v33253-pwa-toast';
el.setAttribute('role','status');
el.setAttribute('aria-live','polite');
document.body.appendChild(el);
}
el.className='v33253-pwa-toast is-'+(type||'info')+' is-visible';
el.textContent=message;
clearTimeout(toastTimer);
toastTimer=setTimeout(function(){el.classList.remove('is-visible');},3200);
}
function isStandalone(){
return window.matchMedia('(display-mode: standalone)').matches || window.navigator.standalone===true;
}
function buildInstallButton(){
if(isStandalone() || document.getElementById('v33253PwaInstall'))return;
installButton=document.createElement('button');
installButton.type='button';
installButton.id='v33253PwaInstall';
installButton.className='v33253-pwa-install';
installButton.innerHTML='+Instalar aplicativoAcesso rápido na tela inicial';
installButton.hidden=true;
installButton.addEventListener('click',async function(){
if(!deferredPrompt)return;
deferredPrompt.prompt();
try{
var choice=await deferredPrompt.userChoice;
if(choice && choice.outcome==='accepted')toast('Aplicativo instalado com sucesso.','success');
}catch(e){}
deferredPrompt=null;
installButton.hidden=true;
});
document.body.appendChild(installButton);
}
function registerSW(){
if(!('serviceWorker' in navigator) || location.protocol!=='https:')return;
var swUrl=new URL('index.php?entity=pwa&page=sw&v=33_3_5',document.baseURI).href;
navigator.serviceWorker.register(swUrl,{scope:'./',updateViaCache:'none'}).then(function(reg){
if(reg.waiting)toast('Nova versão pronta para atualizar.','info');
reg.addEventListener('updatefound',function(){
var worker=reg.installing;
if(!worker)return;
worker.addEventListener('statechange',function(){
if(worker.state==='installed' && navigator.serviceWorker.controller){
toast('Nova versão disponível. Reabra o app para atualizar.','info');
}
});
});
}).catch(function(err){
console.warn('PWA service worker:',err);
});
}
ready(function(){
buildInstallButton();
registerSW();
window.addEventListener('beforeinstallprompt',function(event){
event.preventDefault();
deferredPrompt=event;
if(installButton)installButton.hidden=false;
});
window.addEventListener('appinstalled',function(){
deferredPrompt=null;
if(installButton)installButton.hidden=true;
toast('Aplicativo instalado.','success');
});
window.addEventListener('offline',function(){
document.documentElement.classList.add('is-pwa-offline');
toast('Você está offline. Algumas ações precisam de internet.','warning');
});
window.addEventListener('online',function(){
document.documentElement.classList.remove('is-pwa-offline');
toast('Conexão restaurada.','success');
});
if(!navigator.onLine)document.documentElement.classList.add('is-pwa-offline');
});
})();