import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{Hn as n}from"./DIQmuJJZ.js";import{t as r}from"./CxlOpib82.js";import{r as i,u as a}from"./CX8DOZuw2.js";import{n as o}from"./DeO5NWhN.js";import{t as s}from"./C0nwgWmB.js";import{t as c}from"./OnYyYdSr2.js";var l=e(t(),1);function u(e){let t=a(e),n=(0,l.join)(i(t),`project-info.json`);if(!(0,l.existsSync)(n))return null;try{let e=(0,l.readFileSync)(n,`utf-8`),r=JSON.parse(e);if(typeof r.projectHash!=`string`||r.projectHash.length===0)return null;let i=t.replace(/[\\/]+$/,``).split(/[\\/]/),a=i[i.length-1]??`unknown`;return{projectHash:r.projectHash,projectId:typeof r.projectId==`string`?r.projectId:``,projectRoot:t,projectName:a}}catch{return null}}function d(e){return e&&typeof e==`object`&&`$client`in e?e.$client??e:e}function f(e){return!!(e&&typeof e==`object`&&`exec`in e&&`prepare`in e)}var p=new Set([`global`]);function m(e,t,i){if(!p.has(e)||!f(t))return;let o=u(a(i));if(!o||o.projectId.length===0)return;let s;try{s=t.prepare(`SELECT project_id, project_path FROM nexus_project_registry WHERE project_path = ? LIMIT 1`).get(o.projectRoot)}catch{return}if(!(!s||typeof s.project_id!=`string`||s.project_id.length===0)&&s.project_id!==o.projectId)throw new r(n.CONFIG_ERROR,`E_PROJECT_ID_DRIFT: ${e} DB reports project_id=${s.project_id} for path=${o.projectRoot} but .cleo/project-info.json reports projectId=${o.projectId}. The DB and project-info.json are not pointing at the same project — a backup/restore or directory move has left them inconsistent.`,{fix:`Inspect both values: \`cat ${o.projectRoot}/.cleo/project-info.json\` and the corresponding row in nexus.db's project_registry. Reconcile by either (a) updating project-info.json to the canonical ID, or (b) re-registering this project with \`cleo init --reset\` if the registry entry is stale.`})}async function h(e,t){e===`project`&&c(`tasks`,t);let n=e===`project`?await o(`project`,t):await o(`global`,t),r=d(n.db);return f(r)&&s(r),m(e,r,t),{db:r,role:e,async close(){n.close()}}}function g(e,t={}){let{readOnly:n=!0,applyPragmas:r=!0}=t,{DatabaseSync:i}=(0,l.createRequire)(import.meta.url)(`node:sqlite`),a=new i(e,{readOnly:n});r&&s(a,{enableWal:!n});let o=!1;return{db:a,path:e,close(){if(!o){o=!0;try{a.close()}catch{}}}}}export{g as n,m as r,h as t};