import{b as y,g as p,v as E}from"./auth-C4kx9BHk.js";import{s as I,c as B}from"./items-BcXKT9tZ.js";y({get:e=>chrome.storage.local.get(e),set:e=>chrome.storage.local.set(e),remove:e=>chrome.storage.local.remove(e)});const d={notConnected:document.getElementById("state-not-connected"),notLoggedIn:document.getElementById("state-not-logged-in"),loading:document.getElementById("state-loading"),ready:document.getElementById("state-ready"),saving:document.getElementById("state-saving"),saved:document.getElementById("state-saved"),alreadySaved:document.getElementById("state-already-saved"),error:document.getElementById("state-error")},f=document.getElementById("footer"),h=document.getElementById("footer-username"),m=document.getElementById("save-btn"),x=document.getElementById("retry-btn"),w=document.getElementById("view-saved-btn"),C=document.getElementById("view-existing-btn"),L=document.getElementById("open-options-connect"),b=document.getElementById("open-options-login"),S=document.getElementById("open-settings"),c=document.getElementById("page-title"),l=document.getElementById("page-url"),k=document.getElementById("saved-subtitle"),u=document.getElementById("already-saved-title"),T=document.getElementById("error-detail");let n={url:"",title:""},a="",r="";function t(e){for(const s of Object.values(d))s.classList.remove("active");d[e].classList.add("active")}function O(e){f.style.display="flex",h.textContent=e}function g(e){chrome.tabs.create({url:e})}L.addEventListener("click",()=>chrome.runtime.openOptionsPage());b.addEventListener("click",()=>chrome.runtime.openOptionsPage());S.addEventListener("click",()=>chrome.runtime.openOptionsPage());m.addEventListener("click",()=>v());x.addEventListener("click",()=>v());w.addEventListener("click",()=>{r&&a&&g(`${a}/item/${r}`)});C.addEventListener("click",()=>{r&&a&&g(`${a}/item/${r}`)});async function v(){t("saving");try{const e=await I(n.url,n.title);e.status==="duplicate"?(r=e.existingId,u.textContent=e.title||n.title||"This page",t("alreadySaved")):(r=e.item.id,k.textContent=e.item.title||n.title||"Saved",t("saved"))}catch(e){T.textContent=e instanceof Error?e.message:"An unexpected error occurred",t("error")}}async function U(){t("loading");const e=await p();if(a=e.serverUrl,!a){t("notConnected");return}if(!e.token){t("notLoggedIn");return}const s=await E();if(!s){t("notLoggedIn");return}O(s.username);const[o]=await chrome.tabs.query({active:!0,currentWindow:!0});if(!(o!=null&&o.url)||o.url.startsWith("chrome://")||o.url.startsWith("chrome-extension://")){c.textContent="Cannot save this page",l.textContent="Browser internal pages are not supported",t("ready"),m.disabled=!0;return}n={url:o.url,title:o.title||""},c.textContent=n.title||n.url,l.textContent=n.url;try{const i=await B(n.url);if(i.exists&&i.item_id){r=i.item_id,u.textContent=i.title||n.title||"This page",t("alreadySaved");return}}catch{}t("ready")}U();
