import"./modulepreload-polyfill-B5Qt9EMX.js";import{S as v}from"./messages-CjQo1qp0.js";import{S as n}from"./storage-pXeRtiJs.js";document.addEventListener("DOMContentLoaded",()=>{const g=document.getElementById("baseUrl"),s=document.getElementById("apiKey"),a=document.getElementById("toggleApiKey"),m=document.getElementById("projectId"),p=document.getElementById("projectName"),y=document.getElementById("category"),f=document.getElementById("saveButton"),h=document.getElementById("openSidebarButton"),c=document.getElementById("status"),r=(t,e=2500)=>{c&&(c.textContent=t,e>0&&setTimeout(()=>{c.textContent===t&&(c.textContent="")},e))},i="https://cogniz.online";chrome.storage.sync.get([n.PLATFORM_URL,n.API_KEY,n.SELECTED_PROJECT,n.SELECTED_PROJECT_NAME,n.DEFAULT_CATEGORY],t=>{g.value=i,(t[n.PLATFORM_URL]||"")!==i&&chrome.storage.sync.set({[n.PLATFORM_URL]:i});const o=t[n.API_KEY]||"";o&&o.length>6?(s.value=o.substring(0,6)+"************",s.dataset.fullKey=o):s.value=o,m.value=t[n.SELECTED_PROJECT]||"",p.value=t[n.SELECTED_PROJECT_NAME]||"",y.value=t[n.DEFAULT_CATEGORY]||"general"});const C=()=>{const t=i;let e=s.value.trim();s.dataset.fullKey&&e.includes("****")&&(e=s.dataset.fullKey);const o=m.value.trim(),l=p.value.trim(),E=y.value.trim()||"general";if(!e||!o){r("API key and project ID are required.");return}if(e.length<10){r("API key appears too short. Check your Cogniz settings.");return}chrome.storage.sync.set({[n.PLATFORM_URL]:t,[n.API_KEY]:e,[n.SELECTED_PROJECT]:o,[n.SELECTED_PROJECT_NAME]:l,[n.DEFAULT_CATEGORY]:E,[n.MEMORY_ENABLED]:!0},()=>{r("Configuration saved successfully!"),e.length>6&&(s.value=e.substring(0,6)+"************",s.dataset.fullKey=e),T()})},T=async()=>{const t=i;let e=s.value.trim();if(s.dataset.fullKey&&e.includes("****")&&(e=s.dataset.fullKey),!e)return;const o=t.replace(/\/+$/,""),l=`${o}/wp-json/memory/v1/debug-settings`,E=`${o}/wp-json/memory/v1/user-stats`;try{r("Testing connection...",0);const u=await fetch(l,{method:"GET",headers:{Authorization:`Bearer ${e}`,"X-Memory-Platform-Client":"browser-extension"}});if(!u.ok){r(`Cogniz site not accessible: ${u.status}`);return}const d=await fetch(E,{method:"GET",headers:{Authorization:`Bearer ${e}`,"X-Memory-Platform-Client":"browser-extension","Content-Type":"application/json"}});d.ok?r("Connection successful!"):d.status===401?r("Unauthorized: verify the API key generated in WordPress."):d.status===404?r("Memory Platform plugin route not found."):r(`Connection failed: HTTP ${d.status}`)}catch(u){console.error("[popup] Connection error:",u),r("Connection error – check URL and ensure the site is reachable.")}};f?.addEventListener("click",t=>{t.preventDefault(),C()}),a?.addEventListener("click",t=>{t.preventDefault(),s.type==="password"?(s.type="text",a.textContent="🙈",a.title="Hide API Key"):(s.type="password",a.textContent="👁️",a.title="Show API Key")}),h?.addEventListener("click",t=>{t.preventDefault(),chrome.tabs.query({active:!0,currentWindow:!0},e=>{const o=e[0]?.id;o!=null?chrome.tabs.sendMessage(o,{action:v.TOGGLE_SIDEBAR},l=>{if(chrome.runtime.lastError){console.error("[popup] Error sending message:",chrome.runtime.lastError.message),r("Unable to open sidebar on this page. Try a regular web page.",3e3);return}}):r("No active tab found.",2e3),setTimeout(()=>{window.close()},100)})})});
