import{b as f,r as d,a as R,s as E}from"./mem0_proxy-DKi2ici5.js";import{T as a,M as g,S as m}from"./messages-CjQo1qp0.js";import{S as s}from"./storage-pXeRtiJs.js";var c=(e=>(e.BOOKMARK="BOOKMARK",e.NAVIGATION="NAVIGATION",e.SEARCH="SEARCH",e))(c||{});function T(){chrome.contextMenus.removeAll(()=>{try{chrome.contextMenus.create({id:"mem0.saveSelection",title:"Save selection to Memory Platform",contexts:["selection"]},()=>{const e=chrome.runtime.lastError;e&&console.warn("[context-menu] Create error:",e.message)})}catch(e){console.warn("[context-menu] Failed to create menu:",e)}}),chrome.contextMenus.onClicked.addListener(async(e,r)=>{if(!r||r.id===null||r.id===void 0||e.menuItemId!=="mem0.saveSelection")return;const t=r.id,n=String(e.selectionText||"").trim();if(!n){i(t,"Select text first",a.ERROR);return}const o=await f();if(!o){i(t,"Configure the Memory Platform extension first",a.ERROR);return}if(o.memoryEnabled===!1){i(t,"Memory capture is disabled",a.ERROR);return}const h=r.title||"",y=e.pageUrl||r.url||"";let u=await l(t);u&&u.error&&(await w(t),u=await l(t));const S=M({selection:n,title:h,url:y});try{await d(S,{category:c.BOOKMARK}),i(t,"Saved to Memory Platform",a.SUCCESS),R("context_menu_save",{project_id:o.projectId,category:c.BOOKMARK})}catch(p){console.error("Failed to add memory:",p),i(t,"Failed to store memory",a.ERROR)}})}function i(e,r,t=a.SUCCESS){try{const n={type:g.TOAST,payload:{message:r,variant:t}};chrome.tabs.sendMessage(e,n)}catch{}}function A(e){return(e||"").replace(/\s+/g," ").trim()}function C(e,r){return!e||e.length<=r?e:e.slice(0,r-1).trimEnd()+"…"}function M({selection:e,title:r,url:t}){const o=[`Captured Selection:
${C(A(e),1200)}`];return r&&o.push(`Page Title: ${r}`),t&&o.push(`Page URL: ${t}`),o.join(`

`)}function l(e){return new Promise(r=>{try{chrome.tabs.sendMessage(e,{type:g.GET_SELECTION_CONTEXT},void 0,t=>{r(t||{error:"no-response"})})}catch(t){r({error:String(t)})}})}async function w(e){try{return chrome.scripting?(await chrome.scripting.executeScript({target:{tabId:e},files:["selection_context.ts"]}),!0):!1}catch{return!1}}chrome.storage.sync.get([s.TRACK_SEARCHES],e=>{typeof e[s.TRACK_SEARCHES]>"u"&&chrome.storage.sync.set({[s.TRACK_SEARCHES]:!1})});async function v(){try{const e=await f();return!e||e.memoryEnabled===!1?!1:await new Promise(t=>{chrome.storage.sync.get([s.TRACK_SEARCHES],n=>{t(n[s.TRACK_SEARCHES]===!0)})})}catch{return!1}}async function O(e){await d(e,{category:c.NAVIGATION})}function I(e){if(!e||e.frameId!==0)return!1;const r=e.url||"";if(!/^https?:\/\//i.test(r))return!1;const t=e.transitionType||"",n=e.transitionQualifiers||[];return!!(t==="typed"||n&&n.includes("from_address_bar"))}function _(){try{chrome.webNavigation.onCommitted.addListener(async e=>{try{if(!I(e)||!await v())return;const r=e.url;if(!r||N(r))return;const t=(()=>{try{return new URL(r).hostname}catch{return""}})(),n=b(),o=[`Navigated to: ${r}`,`Timestamp: ${n.date} ${n.time}`];t&&o.splice(1,0,`Domain: ${t}`),await O(o.join(`
`))}catch(r){console.warn("Navigation capture failed",r)}})}catch(e){console.warn("Unable to register navigation listener",e)}}function N(e){try{const r=new URL(e),t=r.hostname||"",n=r.pathname||"",o=r.searchParams||new URLSearchParams;return!!((/^.*\.google\.[^\\/]+$/.test(t)||t==="google.com"||t.endsWith(".google.com"))&&n.startsWith("/search")&&o.get("q")||t.includes("bing.com")&&n.includes("/search")||(t.includes("search.yahoo.com")||t.includes("duckduckgo.com"))&&o.get("q")||t.includes("search.brave.com")&&o.get("q"))}catch{return!1}}function b(){const e=new Date,r=e.toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"}),t=e.toLocaleTimeString(void 0,{hour:"numeric",minute:"2-digit"});return{date:r,time:t}}E();chrome.runtime.onInstalled.addListener(()=>{chrome.storage.sync.set({memory_enabled:!0},()=>{console.log("Memory enabled set to true on install/update")})});chrome.runtime.onMessage.addListener(e=>{e.action===m.OPEN_DASHBOARD&&e.url&&chrome.tabs.create({url:e.url})});chrome.runtime.onMessage.addListener((e,r)=>{if(e.action===m.SIDEBAR_SETTINGS){const t=r.tab?.id;t!=null&&chrome.tabs.sendMessage(t,{action:m.SIDEBAR_SETTINGS})}});T();_();
