const d="698235805836-fdiopq0e75d3j74df54rn5g2phrfv4el.apps.googleusercontent.com",p="https://ilvubumkfsbzbzinwiyt.supabase.co",f="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlsdnVidW1rZnNiemJ6aW53aXl0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjM3NjIyMjIsImV4cCI6MjA3OTMzODIyMn0.pkMg-1TfOSMdrt9i-UAgjkgdCMx3ienYzjK-soEe58A";chrome.sidePanel.setPanelBehavior({openPanelOnActionClick:!0});chrome.runtime.onInstalled.addListener(()=>{console.log("Spotlight extension installed"),chrome.alarms.create("check-notifications",{periodInMinutes:5})});chrome.alarms.create("check-notifications",{periodInMinutes:5});async function _(t){const e=new TextEncoder().encode(t),r=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(r)).map(c=>c.toString(16).padStart(2,"0")).join("")}chrome.alarms.onAlarm.addListener(async t=>{if(t.name==="check-notifications")try{const e=(await chrome.storage.local.get(["supabase_access_token"])).supabase_access_token;if(!e)return;const r=await fetch(`${p}/functions/v1/check-notifications`,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json",apikey:f},body:JSON.stringify({})});if(!r.ok)return;const c=(await r.json()).matches||[];for(const i of c){const a=i.from?.replace(/<[^>]+>/g,"").trim().substring(0,40)||"Unknown";chrome.notifications.create(`spotlight-${i.gmail_message_id}`,{type:"basic",iconUrl:"icons/icon-128.png",title:`New email from ${a}`,message:i.subject||"(no subject)",priority:1})}}catch(o){console.error("[Spotlight] Notification check failed:",o)}});chrome.notifications.onClicked.addListener(async t=>{if(!t.startsWith("spotlight-"))return;const o=await chrome.tabs.query({active:!0,currentWindow:!0});o[0]?.id&&chrome.sidePanel.open({tabId:o[0].id}),chrome.notifications.clear(t)});chrome.runtime.onMessage.addListener((t,o,e)=>{if(t.type==="OPEN_SIDE_PANEL"){const r=o.tab?.id,n=o.tab?.windowId;return r&&n?chrome.sidePanel.open({windowId:n}).then(()=>{e({success:!0})}).catch(c=>{console.error("[Spotlight] Failed to open side panel:",c.message),e({error:c.message})}):e({error:"No tab ID"}),!0}if(t.type==="GET_AUTH_TOKEN")return chrome.identity.getAuthToken({interactive:!0},r=>{chrome.runtime.lastError?e({error:chrome.runtime.lastError.message}):e({token:r})}),!0;if(t.type==="GET_GOOGLE_ID_TOKEN"){const r=crypto.randomUUID(),n=chrome.identity.getRedirectURL(),c=["openid","email","profile","https://www.googleapis.com/auth/gmail.readonly","https://www.googleapis.com/auth/gmail.send"].join(" ");return _(r).then(i=>{const a=new URL("https://accounts.google.com/o/oauth2/v2/auth");a.searchParams.set("client_id",d),a.searchParams.set("redirect_uri",n),a.searchParams.set("response_type","id_token token"),a.searchParams.set("scope",c),a.searchParams.set("nonce",i),a.searchParams.set("prompt","consent"),chrome.identity.launchWebAuthFlow({url:a.toString(),interactive:!0},s=>{if(chrome.runtime.lastError||!s){e({error:chrome.runtime.lastError?.message||"Auth failed"});return}const h=s.indexOf("#");if(h===-1){e({error:"No tokens in response"});return}const u=new URLSearchParams(s.substring(h+1)),l=u.get("id_token"),m=u.get("access_token");if(!l||!m){e({error:"Missing tokens in response"});return}e({idToken:l,accessToken:m,nonce:r})})}),!0}if(t.type==="STORE_SUPABASE_TOKEN")return chrome.storage.local.set({supabase_access_token:t.token}),e({success:!0}),!0;if(t.type==="CLEAR_SUPABASE_TOKEN")return chrome.storage.local.remove("supabase_access_token"),e({success:!0}),!0;if(t.type==="SIGN_OUT")return chrome.storage.local.remove("supabase_access_token"),chrome.identity.clearAllCachedAuthTokens(()=>{e({success:!0})}),!0});
