import{c as e,r as t,t as n}from"./supabase-Dzo4IAah.js";chrome.runtime.onInstalled.addListener(()=>{chrome.contextMenus.create({id:`save-to-sidermem`,title:`保存到 SiderMem 记忆库`,contexts:[`selection`]})});async function r(){let e=await chrome.storage.local.get([`sidermem-settings`]),t=(e[`sidermem-settings`]?JSON.parse(e[`sidermem-settings`]):null)?.state?.language||`auto`;if(t===`auto`){let e=chrome.i18n.getUILanguage().split(`-`)[0].toLowerCase();return[`zh`,`en`,`ar`,`fr`,`de`,`es`,`ko`].includes(e)?e:`en`}return t}chrome.contextMenus.onClicked.addListener(async(e,n)=>{if(e.menuItemId===`save-to-sidermem`&&e.selectionText){let a=t(await r());try{await i({title:`${a.source}: ${n?.title?.substring(0,20)||`web`}...`,contentOriginal:e.selectionText,sourceType:`selection`,sourceUrl:n?.url,tags:[`web-selection`]})}catch(e){console.error(`Failed to save memory from context menu:`,e)}}}),chrome.runtime.onMessage.addListener((e,t,n)=>{if(e.type===`SAVE_MEMORY`)return i({...e.payload,sourceUrl:t.tab?.url,sourceApp:e.payload.sourceApp||(t.tab?.url?.includes(`chatgpt.com`)?`ChatGPT`:`Claude`)}).then(e=>n({success:!0,id:e})).catch(e=>n({success:!1,error:e.message})),!0;if(e.type===`GET_EFFECTIVE_LANGUAGE`)return r().then(e=>n({lang:e})),!0});async function i(t){let{data:{session:r}}=await n.auth.getSession(),i=r?.user?.id||`localuser`,a=await e.memories.add({title:t.title||`New Memory`,contentOriginal:t.contentOriginal||``,sourceType:t.sourceType||`manual`,sourceUrl:t.sourceUrl,sourceApp:t.sourceApp,userId:i,tags:t.tags||[],createdAt:Date.now(),updatedAt:Date.now(),...t});return console.log(`Saved to SiderMem:`,a,`for user:`,i),a}chrome.sidePanel.setPanelBehavior({openPanelOnActionClick:!0}).catch(e=>console.error(e));