import{a as e,r as t}from"./bG-fsHMS.js";import{t as n}from"./CvFbqtf9.js";var r=3,i=[`not`,`never`,`deprecated`,`removed`,`replaced`,`avoid`,`no longer`,`do not`,`don't`,`disabled`,`deleted`,`dropped`,`broken`,`invalid`,`obsolete`,`reverted`,`undone`,`superseded`],a=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(`.`));function o(e){let t=e.toLowerCase().replace(/[^a-z0-9\s\-_]/g,` `).split(/\s+/),n=new Set;for(let e of t)e.length>=4&&!a.has(e)&&n.add(e);return n}function s(e){let t=e.toLowerCase();return i.filter(e=>t.includes(e))}function c(e,t){let n=[];for(let r of e)t.has(r)&&n.push(r);return n}async function l(i){await t(i);let a=e();if(!a)return[];let l=[],d=new Date().toISOString().replace(`T`,` `).slice(0,19);for(let{table:e,textCol:t}of[{table:`brain_observations`,textCol:`narrative`},{table:`brain_learnings`,textCol:`insight`},{table:`brain_patterns`,textCol:`pattern`},{table:`brain_decisions`,textCol:`decision`}]){if(l.length>=50)break;let i=[];try{i=n(a.prepare(`
          SELECT id, COALESCE(${t}, '') AS text, quality_score
          FROM ${e}
          WHERE invalid_at IS NULL
            AND verified = 1
          LIMIT 200
        `))}catch{continue}if(i.length<2)continue;let f=new Map,p=new Map;for(let e of i)f.set(e.id,o(e.text)),p.set(e.id,s(e.text));for(let t=0;t<i.length&&!(l.length>=50);t++){let n=i[t],o=f.get(n.id),s=p.get(n.id);for(let m=t+1;m<i.length&&!(l.length>=50);m++){let t=i[m],h=f.get(t.id),g=p.get(t.id),_=c(o,h);if(_.length<r)continue;let v=s.filter(e=>!g.includes(e)),y=g.filter(e=>!s.includes(e));if(!(v.length>0||y.length>0))continue;let b=[...v,...y],x=(n.quality_score??.5)<=(t.quality_score??.5)?n.id:t.id;x===n.id?t.id:n.id,l.push({entryAId:n.id,entryBId:t.id,contradictedId:x,table:e,sharedKeywords:_.slice(0,10),negationMarkers:b.slice(0,5)});let S=u(e,n.id),C=u(e,t.id);try{a.prepare(`
              INSERT OR IGNORE INTO brain_page_edges
                (from_id, to_id, edge_type, weight, provenance, created_at)
              VALUES (?, ?, 'contradicts', 0.8, 'consolidation:contradiction', ?)
            `).run(S,C,d),a.prepare(`
              INSERT OR IGNORE INTO brain_page_edges
                (from_id, to_id, edge_type, weight, provenance, created_at)
              VALUES (?, ?, 'contradicts', 0.8, 'consolidation:contradiction', ?)
            `).run(C,S,d)}catch{}try{a.prepare(`
              UPDATE ${e}
              SET quality_score = MAX(0.0, COALESCE(quality_score, 0.5) - 0.15),
                  updated_at = ?
              WHERE id = ?
            `).run(d,x)}catch{}}}}return l}function u(e,t){return`${{brain_observations:`observation`,brain_learnings:`learning`,brain_patterns:`pattern`,brain_decisions:`decision`}[e]??`entry`}:${t}`}function d(e){return e.toLowerCase().replace(/[^\w\s]/g,` `).replace(/\s+/g,` `).trim()}async function f(r,i=3600){await t(r);let a=e();if(!a)return 0;let o=n(a.prepare(`SELECT id, pattern, peer_id, extracted_at, COALESCE(occurrence_count, 1) AS occurrence_count FROM brain_patterns WHERE invalid_at IS NULL ORDER BY peer_id, pattern, extracted_at ASC`)),s=new Map;for(let e of o){let t=`${e.peer_id}::${d(e.pattern)}`,n=s.get(t)??[];n.push(e),s.set(t,n)}let c=[];for(let e of s.values()){if(e.length<2)continue;let t=[],n=[e[0]];for(let r=1;r<e.length;r++){let a=n[n.length-1],o=new Date(a.extracted_at).getTime(),s=new Date(e[r].extracted_at).getTime();Math.abs(s-o)/1e3<=i?n.push(e[r]):(n.length>=2&&t.push(n),n=[e[r]])}n.length>=2&&t.push(n);for(let e of t){let t=e[0],n=e.slice(1);c.push({keptId:t.id,removedIds:n.map(e=>e.id),normalizedTitle:d(t.pattern)})}}if(c.length===0)return 0;let l=0;a.prepare(`BEGIN`).run();try{for(let e of c){let t=n(a.prepare(`SELECT id, pattern, peer_id, extracted_at, COALESCE(occurrence_count, 1) AS occurrence_count FROM brain_patterns WHERE id IN (${e.removedIds.map(()=>`?`).join(`,`)})`),...e.removedIds),r=t.reduce((e,t)=>e+t.occurrence_count,0),i=t.reduce((e,t)=>t.extracted_at>e?t.extracted_at:e,t[0]?.extracted_at??``);a.prepare(`UPDATE brain_patterns SET occurrence_count = occurrence_count + ?, last_seen_at = ? WHERE id = ?`).run(r,i,e.keptId);for(let t of e.removedIds)a.prepare(`DELETE FROM brain_patterns WHERE id = ?`).run(t),l++}a.prepare(`COMMIT`).run()}catch(e){throw a.prepare(`ROLLBACK`).run(),e}return l}export{l as n,f as t};