import{a as e}from"./bG-fsHMS.js";var t=.5,n=0,r=1,i=.92,a=.35,o=50,s=30;function c(e,t){if(e.length!==t.length||e.length===0)return 0;let n=0,r=0,i=0;for(let a=0;a<e.length;a++)n+=(e[a]??0)*(t[a]??0),r+=(e[a]??0)**2,i+=(t[a]??0)**2;let a=Math.sqrt(r)*Math.sqrt(i);return a===0?0:n/a}function l(e,t){return t?e*.5**((Date.now()-new Date(t).getTime())/(1e3*60*60*24)/s):e}function u(e){let t=new Float32Array(e.buffer,e.byteOffset,e.byteLength/4);return Array.from(t)}function d(e,t,n,r){return e.prepare(`SELECT e.id, e.embedding, o.created_at
       FROM brain_embeddings e
       JOIN brain_observations o ON o.id = e.id
       WHERE e.id != ?
         AND e.embedding IS NOT NULL
       ORDER BY o.created_at DESC
       LIMIT ?`).all(t,o).filter(e=>e.embedding!==null&&e.embedding.byteLength>0).map(e=>({id:e.id,embedding:u(e.embedding),createdAt:e.created_at}))}function f(o,s={}){let{db:u,project:f=null,peerId:p=`global`}=s,m=u===void 0?e():u;if(!o.embedding||o.embedding.length===0)return m||console.warn(`[surprisal] No database available; returning neutral score.`),{id:o.id,score:t,embeddingAvailable:!1};if(!m)return console.warn(`[surprisal] Database not initialized; returning neutral score.`),{id:o.id,score:t,embeddingAvailable:!1};try{let e=d(m,o.id,f,p);if(e.length===0)return{id:o.id,score:r,embeddingAvailable:!0,maxSimilarity:0};let t=0,s;for(let n of e){let e=l(c(o.embedding,n.embedding),n.createdAt);e>t&&(t=e,s=n.id)}let u;return u=t>=i?n+(1-t)*.3:t<=a?.7+(1-t)*.3:.7-(t-a)/(i-a)*.4,u=Math.max(n,Math.min(r,u)),{id:o.id,score:u,embeddingAvailable:!0,mostSimilarId:s,maxSimilarity:t}}catch(e){return console.warn(`[surprisal] Error computing score; returning neutral:`,e),{id:o.id,score:t,embeddingAvailable:!1}}}function p(e,t={}){return e.map(e=>f(e,t)).sort((e,t)=>t.score-e.score)}export{t as NEUTRAL_SURPRISAL,p as computeSurprisalBatch,f as computeSurprisalScore};