import{_ as A}from"./chunks/preload-helper-Du-ZIQLl.js";const w=window.FindAstraConfig||{},g=document.querySelector("[data-findastra-admin]");if(g){const i=g.querySelector("[data-findastra-index-button]"),d=g.querySelector("[data-findastra-reset-button]"),y=g.querySelector("[data-findastra-progress]"),S=g.dataset.serverSide==="1",$=g.dataset.backgroundIndex==="1";let l=null;const t=e=>{y&&(y.textContent=e)},o=async(e,s={})=>{const r=w.rest.root.replace(/\/$/,"")+e,n={"Content-Type":"application/json","X-WP-Nonce":w.rest.nonce,...s.headers||{}},u=await fetch(r,{credentials:"same-origin",...s,headers:n});if(!u.ok){let f=await u.text();try{const c=JSON.parse(f);c&&c.message&&(f=c.message)}catch{}throw new Error(`${u.status}: ${f}`)}return u.json()};let m=null;const k=()=>(m||(m=A(()=>import("./chunks/embedder-CEK0uOnA.js"),[],import.meta.url)),m),_=async()=>{const{embed:e}=await k();let s=0,r=0;for(;;){t(`Fetching next batch (cursor ${s})…`);const n=await o(`/index/needs?cursor=${s}&batch_size=16`,{method:"GET"});if(n.done)break;if(s=n.next_cursor||s,n.items.length===0){if(n.remaining===0)break;continue}t(`Embedding ${n.items.length} product(s)…`);const u=n.items.map(a=>a.text),f=await e(u,a=>{a&&a.status==="progress"&&a.file&&typeof a.progress=="number"&&t(`Loading model · ${a.file} · ${a.progress.toFixed(1)}%`)}),c=n.items.map((a,B)=>({product_id:a.product_id,text_hash:a.text_hash,vector:f[B],language:a.language||""}));t(`Saving ${c.length} vectors…`);const O=await o("/index/batch",{method:"POST",body:JSON.stringify({embeddings:c})});r+=O.accepted||0,t(`Indexed ${r} so far · ${n.remaining} remaining`)}return r},E=async()=>{let e=0,s=0;for(;;){t(`Embedding batch (cursor ${e})…`);const r=await o(`/index/server-batch?cursor=${e}&batch_size=16`,{method:"POST",body:JSON.stringify({})});if(e=r.next_cursor||e,s+=r.accepted||0,r.done||r.accepted===0&&r.remaining===0)break;t(`Indexed ${s} so far · ${r.remaining} remaining`)}return s},h=2e3,x=e=>{const s=e.status||"idle";if(s==="running"){let n=`Indexing in the background… ${typeof e.percent=="number"?e.percent:0}% (${e.processed||0} / ${e.total||0}). You can safely close this tab — it keeps going.`;e.message&&(n+=` · ${e.message}`),t(n)}else s==="done"?t(`Done. Indexed ${e.indexed||0} products.`):s==="cancelled"?t("Indexing cancelled."):s==="error"&&t(`Error: ${e.message||"background indexing failed"}`)},b=()=>{l&&(clearTimeout(l),l=null)},p=async()=>{try{const e=await o("/index/background/status",{method:"GET"});x(e),(e.status||"")==="running"?(i.disabled=!0,d.disabled=!1,l=setTimeout(p,h)):(b(),i.disabled=!1,d.disabled=!1)}catch(e){b(),t(`Error: ${e.message}`),i.disabled=!1,d.disabled=!1}},I=async()=>{b(),i.disabled=!0,d.disabled=!0,t("Starting background index…");try{const e=await o("/index/background/start",{method:"POST",body:JSON.stringify({reset:!0})});x(e),d.disabled=!1,l=setTimeout(p,h)}catch(e){t(`Error: ${e.message}`),i.disabled=!1,d.disabled=!1}},T=async()=>{try{const e=await o("/index/background/status",{method:"GET"});(e.status||"")==="running"&&(x(e),i.disabled=!0,l=setTimeout(p,h))}catch{}},v=async()=>{if($){I();return}i.disabled=!0,d.disabled=!0,t("Starting…");try{const e=S?await E():await _();t(`Done. Indexed ${e} products this run.`)}catch(e){console.error(e),t(`Error: ${e.message}`)}finally{i.disabled=!1,d.disabled=!1}},P=async()=>{if(confirm("Reset the FindAstra index? You will need to re-index.")){b(),i.disabled=!0,d.disabled=!0,t("Resetting…");try{await o("/index/reset",{method:"POST",body:JSON.stringify({})}),t("Index reset. Click Index now to rebuild.")}catch(e){t(`Error: ${e.message}`)}finally{i.disabled=!1,d.disabled=!1}}};i?.addEventListener("click",v),d?.addEventListener("click",P),$&&T()}
//# sourceMappingURL=admin.js.map
