import{c as m}from"./config.js";document.addEventListener("DOMContentLoaded",()=>{const a=document.getElementById("generateMCPBtn"),l=document.getElementById("mcpStatus"),o=document.getElementById("mcpInfo"),t=document.getElementById("mcpDetails"),d=document.getElementById("copyBtn"),i=document.getElementById("signOutBtn");function r(n,e){if(!l)return;l.textContent=n;const s=`
      margin-top: 10px;
      font-size: 12px;
      padding: 8px 10px;
      border-radius: 6px;
      display: block;
    `;e==="error"?l.style.cssText=s+`
        background: rgba(239,68,68,.1);
        color: #ef4444;
        border: 1px solid rgba(239,68,68,.3);
      `:l.style.cssText=s+`
        background: rgba(55,149,189,.1);
        color: #3795BD;
        border: 1px solid rgba(55,149,189,.3);
      `}function h(){l&&(l.style.display="none")}async function f(){if(!(!a||!o||!t))try{h();const n=await chrome.runtime.sendMessage({action:"CHECK_AUTH"});if(!n.isAuthenticated||!n.user){r("Please sign in to generate MCP instances","error");return}const e=await chrome.runtime.sendMessage({action:"GET_SESSION"});if(!e.success||!e.session){r("Unable to get session. Please sign in again.","error");return}a.disabled=!0,a.innerHTML="<span>Generating...</span>",r("Generating MCP instance...","loading");const s=await fetch(`${m.apiUrl}/mcp/create`,{method:"POST",headers:{Authorization:`Bearer ${e.session.access_token}`,"Content-Type":"application/json"},body:"{}"});if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const c=await s.json();if(!c.success)throw new Error(c.message||"Failed to create MCP instance");for(o.style.display="block";t.firstChild;)t.removeChild(t.firstChild);const u=document.createElement("strong");u.textContent="User ID:",t.appendChild(u),t.appendChild(document.createTextNode(" "+String(c.user_id??""))),t.appendChild(document.createElement("br"));const g=document.createElement("strong");g.textContent="MCP URL:",t.appendChild(g),t.appendChild(document.createTextNode(" "+String(c.mcp_url??""))),t.appendChild(document.createElement("br"));const p=document.createElement("strong");p.textContent="Status:",t.appendChild(p),t.appendChild(document.createTextNode(" Active")),o.dataset.mcpUrl=c.mcp_url??"",r("MCP instance created successfully!","success")}catch(n){console.error("Error generating MCP instance:",n);const e=n instanceof Error?n.message:"Unexpected error";r(`Error: ${e}`,"error")}finally{a&&(a.disabled=!1,a.innerHTML='<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5v14M5 12h14"/></svg> Generate MCP Instance')}}async function C(){if(!o||!d)return;const n=o.dataset.mcpUrl;if(n)try{await navigator.clipboard.writeText(n);const e=d.textContent||"";d.textContent="Copied!",setTimeout(()=>{d.textContent=e},1500)}catch{}}a?.addEventListener("click",f),d?.addEventListener("click",C),i&&o&&t&&i.addEventListener("click",async()=>{const n=i.textContent||"";i.disabled=!0,i.textContent="Signing out...";try{try{const e=await chrome.runtime.sendMessage({action:"GET_SESSION"});if(e.success&&e.session){const s=await fetch(`${m.apiUrl}/logout`,{method:"POST",headers:{Authorization:`Bearer ${e.session.access_token}`,"Content-Type":"application/json"}});if(!s.ok)console.error("Server logout failed:",s.status,s.statusText),r("Warning: Server sign-out failed, but local data cleared.","error");else{const c=await s.json();console.log("Server logout successful:",c.message)}}}catch(e){console.error("Server logout error:",e),r("Warning: Server sign-out failed, but local data cleared.","error")}if(await chrome.runtime.sendMessage({action:"LOGOUT"}),await chrome.storage.local.clear(),"caches"in window){const e=await caches.keys();await Promise.all(e.map(s=>caches.delete(s)))}for(o.style.display="none",delete o.dataset.mcpUrl;t.firstChild;)t.removeChild(t.firstChild);r("Signed out successfully.","success")}catch(e){console.error("Sign out failed:",e),r("Sign out failed.","error")}finally{i.disabled=!1,i.textContent=n||"Sign Out"}})});
//# sourceMappingURL=settings.js.map
