constput=(key,value)=>db.prepare('INSERT INTO settings VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value').run(key,JSON.stringify(value));
if(!getRaw('policy')){constp=defaultPolicy();put('policy',p);db.prepare('INSERT INTO policies VALUES(?,?,?,?)').run(p.version,newDate().toISOString(),'bootstrap',JSON.stringify(p));}
db.prepare('INSERT OR IGNORE INTO policy_group_history(version,groups) VALUES(?,?)').run(getRaw('policy').version,JSON.stringify(getRaw('policyGroups')||[]));
db.prepare('INSERT OR IGNORE INTO user_profile_history(version,configuration) VALUES(?,?)').run(getRaw('policy').version,JSON.stringify(getRaw('userProfiles')||{profiles:[],categories:[]}));
constinsert=(eventKind,eventMetadata)=>{constresult=db.prepare('INSERT INTO events(time,device_id,kind,metadata) VALUES(?,?,?,?)').run(time,device,eventKind,JSON.stringify(eventMetadata));auditIntegrity.append(db.prepare('SELECT * FROM events WHERE id=?').get(Number(result.lastInsertRowid)));records.push({kind:eventKind,metadata:eventMetadata,id:String(result.lastInsertRowid)});returnNumber(result.lastInsertRowid);};
functionpersonalBookmarks(deviceId){constrow=db.prepare('SELECT revision,items FROM bookmarks WHERE device_id=?').get(deviceId);returnrow?{revision:row.revision,items:JSON.parse(row.items)}:{revision:0,items:[]};}
functioneffectivePolicy(deviceId){letp=get('policy');if(deviceId){constd=db.prepare('SELECT document_analysis_enabled FROM devices WHERE id=?').get(deviceId);if(d?.document_analysis_enabled!==null&&d?.document_analysis_enabled!==undefined)p.inspection.documentAnalysisEnabled=!!d.document_analysis_enabled;p=PolicySchema.parse(applyPolicyGroups(applyUserProfiles(p,getRaw('userProfiles')||{},db.prepare('SELECT * FROM devices WHERE id=?').get(deviceId)||{id:deviceId}),getRaw('policyGroups')||[],deviceId));}returnp;}