import{h as e,n as t,p as n,t as r}from"./js/folders.j8Vf02Wn.js";var i=null,a=null,o=null;async function s(){if(!i){let o=n();e(o),i=t(),a=r()}return o||=Promise.all([i.init(),a.init()]),o}chrome.runtime.onMessage.addListener((e,t,n)=>{if(e.type===`CAPTURE_TAB`)return c(e.payload).then(n),!0;if(e.type===`CAPTURE_ALL_TABS`)return l(e.payload).then(n),!0;if(e.type===`GET_HISTORY`)return u(e.payload).then(n),!0}),chrome.commands.onCommand.addListener(async e=>{if(e!==`quick-capture`)return;await s();let[t]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!t)return;let n=`Saved to Silo!`,r=!0;if(t.url&&/^https?:\/\//i.test(t.url))try{let e=await i.addLink({url:t.url,title:t.title,favicon:t.favIconUrl,folderId:null});e?.link?await i.save():e?.duplicate&&(n=`Already saved — not added again`)}catch(e){console.error(`Quick capture failed:`,e),n=`Failed to save this tab`,r=!1}else n=`This tab can't be saved`,r=!1;try{await chrome.storage.session.set({siloQuickSave:{message:n,ok:r,at:Date.now()}})}catch{}if(chrome.action?.openPopup)try{await chrome.action.openPopup()}catch(e){console.error(`openPopup failed:`,e)}else console.warn(`chrome.action.openPopup is not supported in this browser`)});async function c({tabId:e,targetFolderId:t}){await s();try{let n=await chrome.tabs.get(e);return!n.url||!/^https?:\/\//i.test(n.url)?{success:!1,error:`Cannot capture this tab`}:{success:!0,link:await i.addLink({url:n.url,title:n.title,favicon:n.favIconUrl,folderId:t||null})}}catch(e){return console.error(`Capture tab failed:`,e),{success:!1,error:e.message}}}async function l({windowId:e,targetFolderId:t}){await s();try{let n=(await chrome.tabs.query({windowId:e||(await chrome.windows.getCurrent()).id})).filter(e=>e.url&&/^https?:\/\//i.test(e.url)),r=await Promise.all(n.map(e=>i.addLink({url:e.url,title:e.title,favicon:e.favIconUrl,folderId:t||null})));return{success:!0,links:r,count:r.length}}catch(e){return console.error(`Capture all tabs failed:`,e),{success:!1,error:e.message}}}async function u({search:e,startTime:t,endTime:n,maxResults:r=100}){try{return{success:!0,results:await chrome.history.search({text:e||``,startTime:t,endTime:n,maxResults:r})}}catch(e){return console.error(`Get history failed:`,e),{success:!1,error:e.message}}}chrome.runtime.onInstalled.addListener(()=>{chrome.contextMenus.create({id:`silo-capture-link`,title:`Save to Silo`,contexts:[`link`]}),chrome.contextMenus.create({id:`silo-capture-page`,title:`Save page to Silo`,contexts:[`page`]})}),chrome.contextMenus.onClicked.addListener(async(e,t)=>{await s(),e.menuItemId===`silo-capture-link`&&e.linkUrl?await i.addLink({url:e.linkUrl,title:e.linkText||e.linkUrl,folderId:null}):e.menuItemId===`silo-capture-page`&&t&&await c({tabId:t.id})}),console.log(`Silo background service worker loaded`);