var l=new Map,s=1e3*60*60*24;chrome.runtime.onInstalled.addListener(t=>{t.reason==="install"&&chrome.storage.local.set({skeetData:{initialized:!0,timestamp:new Date().toISOString()}})});async function d(t){try{let o=l.get(t);if(o&&Date.now()-o.timestamp{o.status==="complete"&&e.url&&u(t)});chrome.tabs.onActivated.addListener(({tabId:t})=>{u(t)});var i=[],f=100;function a(t,o,e){let r={timestamp:new Date().toISOString(),type:t,message:o,data:typeof e=="object"?JSON.parse(JSON.stringify(e)):e};i.length>f&&i.shift()}chrome.runtime.onMessage.addListener((t,o,e)=>{try{if(a("info",`Received message of type: ${t.type}`,{message:t,sender:o?{id:o.id,url:o.url,origin:o.origin,tab:o.tab?{id:o.tab.id,url:o.tab.url}:null}:"unknown"}),t.type==="log_to_background")a(t.logType||"info",t.message,t.data),e({success:!0});else if(t.type==="get_background_logs")e({success:!0,logs:i});else if(t.type==="oauth_callback_log")a("info","Received OAuth callback log from content script",t.data),e({success:!0});else{if(t.type==="getData")return chrome.storage.local.get("skeetData",r=>{e({success:!0,data:r.skeetData})}),!0;if(t.type==="popup_oauth_callback"){a("info","Received popup OAuth callback from content script",t.data);try{chrome.runtime.sendMessage({type:"popup_oauth_callback",data:t.data},r=>{if(chrome.runtime.lastError){a("error","Error forwarding popup_oauth_callback",chrome.runtime.lastError);let c={...t.data,timestamp:Date.now()};a("info","Storing OAuth callback data",c),chrome.storage.local.set({oauth_callback_data:c},()=>{chrome.storage.local.get(["oauth_callback_data"],n=>{n&&n.oauth_callback_data?a("info","Successfully stored and verified OAuth callback data",n.oauth_callback_data):a("error","Failed to verify stored OAuth callback data",n)})})}else a("info","Successfully forwarded popup_oauth_callback",r)})}catch(r){a("error","Exception forwarding popup_oauth_callback",r);let c={...t.data,timestamp:Date.now()};a("info","Storing OAuth callback data after exception",c),chrome.storage.local.set({oauth_callback_data:c},()=>{chrome.storage.local.get(["oauth_callback_data"],n=>{n&&n.oauth_callback_data?a("info","Successfully stored and verified OAuth callback data after exception",n.oauth_callback_data):a("error","Failed to verify stored OAuth callback data after exception",n)})})}e({success:!0})}else{if(t.type==="initiate_oauth")return a("info","Initiating OAuth flow",t.data),initiateOAuth(t.data,e),!0;if(t.type==="auth_complete"){a("info","Authentication complete, notifying extension pages",t.data);try{chrome.runtime.sendMessage({type:"auth_status_update",data:{authenticated:!0,did:t.data.did}},r=>{chrome.runtime.lastError?a("info","No listeners for auth_status_update (expected if popup is closed)",chrome.runtime.lastError):a("info","Successfully sent auth_status_update",r)})}catch(r){a("error","Exception sending auth_status_update",r)}e({success:!0})}else{if(t.type==="get_auth_status")return a("info","Getting auth status"),getAuthStatus(e),!0;if(t.type==="logout")return a("info","Logging out user"),logout(e),!0;a("warning","Received unknown message type",{type:t.type}),e({success:!1,error:"Unknown message type"})}}}}catch(r){a("error","Error handling message",{error:r.toString(),stack:r.stack,message:t}),e({success:!1,error:r.toString()})}});async function p(){try{let t=await chrome.storage.local.get(null);return a("info","Current storage contents",t),t}catch(t){return a("error","Error checking storage",t),{}}}p();