import{c as l,K as c}from"./bootstrap-DlNJH-GE.js";const r=a=>({id:a.id,kind:a.kind,name:a.name,url:a.url??void 0,parent_id:a.parent_id??void 0,tags:a.tags_json?JSON.parse(a.tags_json):[],comment:a.comment??void 0,created_at:a.created_at,updated_at:a.updated_at,last_accessed_at:a.last_accessed_at??void 0,source_device:a.source_device??void 0,content_hash:a.content_hash??void 0,perceptual_hash:a.perceptual_hash??void 0,metadata:a.metadata_json?JSON.parse(a.metadata_json):void 0}),_=a=>({id:a.id,kind:a.kind,source_id:a.source_id,target_id:a.target_id,confidence:a.confidence!=null?Number(a.confidence):void 0,created_at:a.created_at,metadata:a.metadata_json?JSON.parse(a.metadata_json):void 0});class E{constructor(t){this.sql=t}async connect(){}async close(){}async upsertNode(t){const i=await this.sql.get(`SELECT id FROM ${l} WHERE id=?`,[t.id]),s=new Date().toISOString(),e=[t.id,t.kind,t.name,t.url??null,t.parent_id??null,JSON.stringify(t.tags??[]),t.comment??null,t.created_at??s,s,t.last_accessed_at??null,t.source_device??null,t.content_hash??null,t.perceptual_hash??null,null,t.metadata?JSON.stringify(t.metadata):null];i?await this.sql.run(`UPDATE ${l} SET kind=?, name=?, url=?, parent_id=?, tags_json=?, comment=?, updated_at=?, last_accessed_at=?, source_device=?, content_hash=?, perceptual_hash=?, metadata_json=? WHERE id=?`,[t.kind,t.name,t.url??null,t.parent_id??null,JSON.stringify(t.tags??[]),t.comment??null,s,t.last_accessed_at??null,t.source_device??null,t.content_hash??null,t.perceptual_hash??null,t.metadata?JSON.stringify(t.metadata):null,t.id]):await this.sql.run(`INSERT INTO ${l} (id, kind, name, url, parent_id, tags_json, comment, created_at, updated_at, last_accessed_at, source_device, content_hash, perceptual_hash, deleted_at, metadata_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,e)}async upsertEdge(t){await this.sql.get(`SELECT id FROM ${c} WHERE id=?`,[t.id])?await this.sql.run(`UPDATE ${c} SET kind=?, source_id=?, target_id=?, confidence=?, metadata_json=? WHERE id=?`,[t.kind,t.source_id,t.target_id,t.confidence!=null?String(t.confidence):null,t.metadata?JSON.stringify(t.metadata):null,t.id]):await this.sql.run(`INSERT INTO ${c} (id, kind, source_id, target_id, confidence, created_at, metadata_json) VALUES (?, ?, ?, ?, ?, ?, ?)`,[t.id,t.kind,t.source_id,t.target_id,t.confidence!=null?String(t.confidence):null,t.created_at??new Date().toISOString(),t.metadata?JSON.stringify(t.metadata):null])}async getNode(t){const i=await this.sql.get(`SELECT * FROM ${l} WHERE id=?`,[t]);return i?r(i):void 0}async deleteNode(t){await this.sql.run(`DELETE FROM ${l} WHERE id=?`,[t]),await this.sql.run(`DELETE FROM ${c} WHERE source_id=?`,[t]),await this.sql.run(`DELETE FROM ${c} WHERE target_id=?`,[t])}async edgesOf(t,i){const s=i?.direction??"both",e=[];if(s==="out"||s==="both"){const n=await this.sql.all(`SELECT * FROM ${c} WHERE source_id=?`,[t]);e.push(...n)}if(s==="in"||s==="both"){const n=await this.sql.all(`SELECT * FROM ${c} WHERE target_id=?`,[t]);e.push(...n)}const d=i?.kinds;return e.map(_).filter(n=>!d||d.includes(n.kind))}async neighbors(t,i){const s=await this.edgesOf(t,{direction:i?.direction,kinds:i?.kinds}),e=new Set;for(const n of s)e.add(n.source_id===t?n.target_id:n.source_id);const d=[];for(const n of e){const o=await this.getNode(n);o&&d.push(o)}return i?.limit?d.slice(0,i.limit):d}async children(t){return(await this.sql.all(`SELECT * FROM ${l} WHERE parent_id=? ORDER BY name`,[t])).map(r)}async ancestors(t){const i=[];let s=await this.getNode(t);for(;s?.parent_id;){const e=await this.getNode(s.parent_id);if(!e)break;i.unshift(e),s=e}return i}}export{E as SqlGraphStore};
//# sourceMappingURL=sql_graph-B1zVA8XT.js.map
