const BRANDING_API='./api/index.php';
let brandingCsrf='';
const brandingEsc=v=>String(v??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
async function loadBranding(){const s=await fetch(`${BRANDING_API}?action=session`,{credentials:'same-origin'}).then(r=>r.json());brandingCsrf=s.csrf||brandingCsrf;if(!s.user)return;const r=await fetch(`${BRANDING_API}?action=data`,{credentials:'same-origin'});const j=await r.json();applyBranding(j.data?.settings);}
function applyBranding(s){const name=s?.business_name?.trim()||'AssetDesk';const brand=document.querySelector('.brand');if(brand?.firstChild)brand.firstChild.nodeValue=name;const workspace=document.querySelector('.workspace > div');if(workspace?.firstChild)workspace.firstChild.nodeValue=name;document.title=`${name} — Office Asset Management`;const icon=document.querySelector('.brand-icon');if(icon&&s?.logo){icon.innerHTML=`
`;icon.classList.add('has-business-logo');}}
const brandingObserver=new MutationObserver(()=>requestAnimationFrame(()=>{if(document.querySelector('.brand'))loadBranding().catch(()=>{});}));brandingObserver.observe(document.body,{childList:true,subtree:true});
loadBranding().catch(()=>{});
brandingObserver.disconnect();
let stableBranding=null,stableBrandingLoading=false;
function renderStableBranding(s){if(!s)return;const name=s.business_name?.trim()||'AssetDesk';const brand=document.querySelector('.brand');if(brand?.firstChild&&brand.firstChild.nodeValue!==name)brand.firstChild.nodeValue=name;const workspace=document.querySelector('.workspace > div');if(workspace?.firstChild&&workspace.firstChild.nodeValue!==name)workspace.firstChild.nodeValue=name;document.title=`${name} — Office Asset Management`;const icon=document.querySelector('.brand-icon');const key=s.logo||'';if(icon&&icon.dataset.logoKey!==key){icon.dataset.logoKey=key;if(key){icon.innerHTML=`
`;icon.classList.add('has-business-logo');}}}
async function stableLoad(){if(stableBrandingLoading)return;stableBrandingLoading=true;try{const s=await fetch(`${BRANDING_API}?action=session`,{credentials:'same-origin'}).then(r=>r.json());if(!s.user)return;const d=await fetch(`${BRANDING_API}?action=data`,{credentials:'same-origin'}).then(r=>r.json());stableBranding=d.data?.settings;renderStableBranding(stableBranding);}finally{stableBrandingLoading=false;}}
const stableObserver=new MutationObserver(()=>requestAnimationFrame(()=>renderStableBranding(stableBranding)));stableObserver.observe(document.body,{childList:true,subtree:true});stableLoad().catch(()=>{});
setInterval(()=>{if(!stableBranding)stableLoad().catch(()=>{});},1500);