import{c as g,b as m,g as u,v as f}from"./auth-C4kx9BHk.js";import{c as p}from"./items-BcXKT9tZ.js";async function y(e){const t=await g(`/api/v1/highlights?item_id=${encodeURIComponent(e)}`);if(!t.ok)throw new Error(`Get highlights failed: ${t.status}`);return t.json()}m({get:e=>chrome.storage.local.get(e),set:e=>chrome.storage.local.set(e),remove:e=>chrome.storage.local.remove(e)});const c={loading:document.getElementById("state-loading"),notConnected:document.getElementById("state-not-connected"),empty:document.getElementById("state-empty"),notSaved:document.getElementById("state-not-saved"),highlights:document.getElementById("state-highlights"),error:document.getElementById("state-error")},l=document.getElementById("page-info"),h=document.getElementById("highlights-list"),v=document.getElementById("error-message"),E=document.getElementById("open-options"),w=document.getElementById("retry-btn");let i="";function o(e){for(const t of Object.values(c))t.classList.remove("active");c[e].classList.add("active")}function B(e){const t=Math.floor((Date.now()-new Date(e).getTime())/1e3);return t<60?"just now":t<3600?`${Math.floor(t/60)}m ago`:t<86400?`${Math.floor(t/3600)}h ago`:`${Math.floor(t/86400)}d ago`}function I(e){h.innerHTML="";for(const t of e){const r=document.createElement("div");r.className="highlight-card",r.style.borderLeftColor=t.color||"#FCD34D";let n=`<p class="highlight-text">“${d(t.text)}”</p>`;n+='<div class="highlight-meta">',n+=`<span class="highlight-color" style="background: ${t.color||"#FCD34D"}"></span>`,n+=`<span class="highlight-time">${B(t.created_at)}</span>`,n+="</div>",t.note&&(n+=`<p class="highlight-note">${d(t.note)}</p>`),r.innerHTML=n,h.appendChild(r)}}function d(e){const t=document.createElement("div");return t.textContent=e,t.innerHTML}async function s(e){if(!e||e.startsWith("chrome://")||e.startsWith("chrome-extension://")){l.textContent="Browser pages not supported",o("empty");return}i=e,o("loading");try{const t=await u();if(!t.serverUrl){o("notConnected");return}if(!t.token){o("notConnected");return}if(!await f()){o("notConnected");return}const n=await p(e);if(!n.exists||!n.item_id){o("notSaved");return}l.textContent=n.title||e;const a=await y(n.item_id);if(a.length===0){o("empty");return}I(a),o("highlights")}catch(t){v.textContent=t instanceof Error?t.message:"Failed to load highlights",o("error")}}E.addEventListener("click",()=>chrome.runtime.openOptionsPage());w.addEventListener("click",()=>{i&&s(i)});chrome.tabs.onActivated.addListener(async({tabId:e})=>{const t=await chrome.tabs.get(e);t.url&&s(t.url)});chrome.tabs.onUpdated.addListener((e,t)=>{t.url&&s(t.url)});async function C(){const[e]=await chrome.tabs.query({active:!0,currentWindow:!0});e!=null&&e.url?s(e.url):o("empty")}C();
