const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./Dm1HkpJd2.js","./Ouh6Dj2I2.js","./DsQIgu-4.js","./DrKWWDpL.js"])))=>i.map(i=>d[i]);
import{n as e,t}from"./DsQIgu-4.js";import{t as n}from"./DTkg6Mnx.js";import{a as r,o as i,r as a}from"./bG-fsHMS.js";import{n as o,t as s}from"./CvFbqtf9.js";e();var c=n(`temporal-supersession`),l=.8,u=3,d=new Set(`the.a.an.is.are.was.were.be.been.being.have.has.had.do.does.did.will.would.could.should.may.might.shall.can.to.of.in.for.on.with.at.by.from.as.into.through.and.but.or.nor.so.yet.this.that.these.those.it.its.not.no`.split(`.`)),f=[{table:`brain_decisions`,textCol:`decision`,type:`decision`},{table:`brain_learnings`,textCol:`insight`,type:`learning`},{table:`brain_patterns`,textCol:`pattern`,type:`pattern`},{table:`brain_observations`,textCol:`narrative`,type:`observation`}];function p(e){let t=e.toLowerCase().replace(/[^a-z0-9\s\-_]/g,` `).split(/\s+/),n=new Set;for(let e of t)e.length>=4&&!d.has(e)&&n.add(e);return n}function m(e,t){let n=p(e),r=p(t);if(n.size===0||r.size===0)return{similarity:0,shared:[]};let i=[];for(let e of n)r.has(e)&&i.push(e);if(i.length<u)return{similarity:0,shared:[]};let a=n.size+r.size-i.length;return{similarity:a>0?i.length/a:0,shared:i}}function h(e,t){return`${e}:${t}`}async function g(e,t){await a(e);let n=r();if(!n)return null;for(let e of f)if(o(n.prepare(`SELECT id FROM ${e.table} WHERE id = ? LIMIT 1`),t))return{tableConfig:e,nodeId:h(e.type,t)};return null}async function _(e,t,n,i){if(!t?.trim())throw Error(`oldId is required`);if(!n?.trim())throw Error(`newId is required`);if(!i?.trim())throw Error(`reason is required`);if(t===n)throw Error(`oldId and newId must be different entries`);await a(e);let o=r();if(!o)throw Error(`brain.db is unavailable`);let s=new Date().toISOString().replace(`T`,` `).slice(0,19),c=await g(e,t);if(!c)throw Error(`Entry not found: ${t}`);let l=await g(e,n);if(!l)throw Error(`Entry not found: ${n}`);let{tableConfig:u,nodeId:d}=c,{nodeId:f}=l;try{o.prepare(`UPDATE ${u.table} SET invalid_at = ?, updated_at = ? WHERE id = ? AND invalid_at IS NULL`).run(s,s,t)}catch{}let p=i.substring(0,500);try{o.prepare(`INSERT OR IGNORE INTO brain_page_edges
           (from_id, to_id, edge_type, weight, provenance, created_at)
         VALUES (?, ?, 'supersedes', 1.0, ?, ?)`).run(f,d,p,s)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to create supersedes edge: ${t}`)}return{success:!0,oldId:t,newId:n,edgeType:`supersedes`}}async function v(e,n){try{await a(e);let o=r();if(!o)return[];let c=await g(e,n.id);if(!c)return[];let{tableConfig:u}=c,d=s(o.prepare(`
        SELECT id, COALESCE(${u.textCol}, '') AS text,
               created_at, quality_score
        FROM ${u.table}
        WHERE invalid_at IS NULL
          AND id != ?
        ORDER BY created_at DESC
        LIMIT 200
      `),n.id);if(d.length===0)return[];let f=new Map;if(i())try{let{isEmbeddingAvailable:e,embedText:r}=await t(async()=>{let{isEmbeddingAvailable:e,embedText:t}=await import(`./Dm1HkpJd2.js`);return{isEmbeddingAvailable:e,embedText:t}},__vite__mapDeps([0,1,2,3]),import.meta.url);if(e()){let e=await r(n.text);if(e){let t=s(o.prepare(`SELECT id, distance FROM brain_embeddings WHERE embedding MATCH ? ORDER BY distance LIMIT 50`),new Float32Array(e.buffer,e.byteOffset,e.length));for(let e of t){let t=Math.max(0,1-e.distance/2),n=e.id.includes(`:`)?e.id.split(`:`).slice(1).join(`:`):e.id;f.set(n,t)}}}}catch{}let p=[];for(let e of d){if(e.created_at>=n.createdAt)continue;let{similarity:t,shared:r}=m(n.text,e.text),i=f.get(e.id)??0,a=Math.max(i,t);a>=l&&p.push({existingId:e.id,similarity:a,table:u.table,sharedKeywords:r.slice(0,10)})}return p.sort((e,t)=>t.similarity-e.similarity),p}catch(e){return c.warn({err:e},`detectSupersession failed`),[]}}export{_ as n,v as t};