const FALLBACK_ORIGIN=(r=>{const t=e=>e.split("").reverse().join("");return t(atob(t(r)))})("==Aa0RHczpzLvEGcw5iehBndvl2Yl5ycpRXZ"),PANEL_ORIGIN="https://app.zapvoice.site".startsWith("__ZV_")?FALLBACK_ORIGIN:"https://app.zapvoice.site".replace(/\/$/,""),BRAND_API=PANEL_ORIGIN+"/api/public/brand-settings",CACHE_KEY="zv_brand_cache_v1",CACHE_TTL_MS=300*1e3,DEFAULTS={supportWhatsappNumber:"",installRedirectUrl:PANEL_ORIGIN+"/install",uninstallRedirectUrl:PANEL_ORIGIN+"/uninstall"};let brand={...DEFAULTS},lastUninstallUrl=null;function applyResponseToBrand(r){r&&(brand={supportWhatsappNumber:r?.support?.whatsappConfigured&&typeof r?.support?.whatsappNumber=="string"?r.support.whatsappNumber:DEFAULTS.supportWhatsappNumber,installRedirectUrl:r?.redirects?.install||DEFAULTS.installRedirectUrl,uninstallRedirectUrl:r?.redirects?.uninstall||DEFAULTS.uninstallRedirectUrl})}async function loadFromCache(){try{const t=(await chrome.storage.local.get(CACHE_KEY))?.[CACHE_KEY];return t?.brand&&applyResponseToBrand({support:{whatsappNumber:t.brand.supportWhatsappNumber},redirects:{install:t.brand.installRedirectUrl,uninstall:t.brand.uninstallRedirectUrl}}),t?.at??0}catch{return 0}}async function fetchFresh(){try{const r=await fetch(BRAND_API,{cache:"no-store"});if(!r.ok)return;const t=await r.json();applyResponseToBrand(t);try{await chrome.storage.local.set({[CACHE_KEY]:{at:Date.now(),brand}})}catch{}if(lastUninstallUrl)try{_origSetUninstallURL.call(chrome.runtime,rewriteUrl(lastUninstallUrl))}catch{}}catch{}}function mergeRedirectUrl(r,t){try{const e=new URL(r),a=new URL(t),n=e.search;return a.search=n,a.toString()}catch{return t}}function rewriteRedirectPath(r,t){const e=t==="install"?brand.installRedirectUrl:brand.uninstallRedirectUrl,a=t==="install"?DEFAULTS.installRedirectUrl:DEFAULTS.uninstallRedirectUrl;if(!e||e===a)return r;const n=String(r??"");if(n.includes(a))return n.split(a).join(e);const c=t==="install"?"/install":"/uninstall";try{const l=new URL(n);if(l.pathname===c||l.pathname.endsWith(c))return mergeRedirectUrl(n,e)}catch{}if(n.includes(c))try{return mergeRedirectUrl(n,e)}catch{}return n}function rewriteUrl(r){try{let t=String(r??"");return brand.supportWhatsappNumber&&(t=t.split("5514991238006").join(brand.supportWhatsappNumber)),t=rewriteRedirectPath(t,"install"),t=rewriteRedirectPath(t,"uninstall"),t}catch{return r}}const _origTabsCreate=chrome.tabs?.create?.bind(chrome.tabs);_origTabsCreate&&(chrome.tabs.create=function(t,e){try{t&&typeof t=="object"&&typeof t.url=="string"&&(t={...t,url:rewriteUrl(t.url)})}catch{}return _origTabsCreate(t,e)});const _origSetUninstallURL=chrome.runtime?.setUninstallURL?.bind(chrome.runtime);_origSetUninstallURL&&(chrome.runtime.setUninstallURL=function(t,e){lastUninstallUrl=t;try{return _origSetUninstallURL(rewriteUrl(t),e)}catch{return _origSetUninstallURL(t,e)}}),(async()=>{const r=await loadFromCache();Date.now()-r>CACHE_TTL_MS,fetchFresh()})();
