const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./Wadww7ZG.js","./DYP3EJMY.js","./DrKWWDpL.js","./DxnOM42y.js","./DsQIgu-4.js","./DTkg6Mnx.js"])))=>i.map(i=>d[i]);
import{s as e}from"./DrKWWDpL.js";import{n as t,t as n}from"./DsQIgu-4.js";import{t as r}from"./DYP3EJMY.js";import{t as i}from"./DTkg6Mnx.js";import{a,o,s}from"./DeO5NWhN.js";var c=e(r(),1);t();function l(){return{}.CLEO_BRAIN_BYPASS_WRITER_THREAD===`1`?(i(`brain-writer`).warn({event:`bypass-active`,envVar:`CLEO_BRAIN_BYPASS_WRITER_THREAD`},`Brain writer-thread chokepoint bypassed via env var — direct main-thread write`),!0):!1}function u(){try{let e=(0,c.join)((0,c.dirname)((0,c.fileURLToPath)(import.meta.url)),`brain-writer-worker.js`);return(0,c.existsSync)(e)?e:null}catch{return null}}var d=class{worker=null;workerReady=!1;workerInitError=null;initPromise=null;pending=new Map;seqCounter=0;shuttingDown=!1;async ensureWorker(){if(!(this.workerReady||this.shuttingDown)){if(this.workerInitError)throw this.workerInitError;if(this.initPromise)return this.initPromise;this.initPromise=this.doInit();try{await this.initPromise}finally{this.initPromise=null}}}async doInit(){let t=u();if(!t)throw this.workerInitError=Error(`brain-writer-worker.js not found adjacent to brain-writer-thread.js`),this.workerInitError;let{Worker:r}=await n(async()=>{let{Worker:t}=await import(`./Wadww7ZG.js`).then(t=>e(t.default,1));return{Worker:t}},__vite__mapDeps([0,1,2]),import.meta.url),a=new r(t);a.on(`message`,e=>{this.handleWorkerMessage(e)}),a.on(`error`,e=>{for(let[,t]of this.pending)t.reject(e instanceof Error?e:Error(String(e)));this.pending.clear(),this.worker=null,this.workerReady=!1,this.workerInitError=e instanceof Error?e:Error(String(e)),i(`brain-writer`).error({err:e},`Brain writer-thread crashed`)}),a.on(`exit`,e=>{if(this.worker=null,this.workerReady=!1,e!==0&&!this.shuttingDown){let t=Error(`brain-writer-worker exited with code ${e}`);for(let[,e]of this.pending)e.reject(t);this.pending.clear(),this.workerInitError=t}}),this.worker=a,this.workerReady=!0}handleWorkerMessage(e){let t=this.pending.get(e.seq);t&&(this.pending.delete(e.seq),e.ok?t.resolve(e.result):t.reject(Error(e.error)))}async enqueue(e){if(this.shuttingDown)throw Error(`brain-writer is shutting down — no new writes accepted`);await this.ensureWorker();let t=this.worker;if(!t)throw Error(`brain-writer worker is not available`);let n=++this.seqCounter,r={seq:n,op:e};return new Promise((e,i)=>{this.pending.set(n,{resolve:e,reject:i});try{t.postMessage(r)}catch(e){this.pending.delete(n),i(e instanceof Error?e:Error(String(e)))}})}async shutdown(){if(this.shuttingDown)return;this.shuttingDown=!0;let e=Date.now();for(;this.pending.size>0&&Date.now()-e<250;)await new Promise(e=>setTimeout(e,25));if(this.worker){try{await this.worker.terminate()}catch{}this.worker=null}this.workerReady=!1,this.pending.clear()}_hasInFlight(){return this.pending.size}},f=`project`,p=`brain`,m=null,h=0,g=null;async function _(){if(s()===`off`)return async()=>{};if(m===null){g===null&&(g=a(f,p,{priority:50}));try{let e=await g;m===null&&(m=e)}finally{g=null}}h+=1;let e=!1;return async()=>{if(!e&&(e=!0,--h,h<=0&&m!==null)){let e=m;m=null,h=0,await e.release()}}}var v=Promise.resolve();function y(e){let t=v.then(()=>b(e));return v=t.catch(()=>void 0),t}async function b(e){o(f,p);let{handleWriteOp:t}=await n(async()=>{let{handleWriteOp:e}=await import(`./DxnOM42y.js`);return{handleWriteOp:e}},__vite__mapDeps([3,2,4,5,1]),import.meta.url);return t(e)}var x=null;function S(){if(!x){x=new d,process.on(`exit`,()=>{x?.shutdown().catch(()=>void 0)});let e=()=>{let e=setTimeout(()=>process.exit(0),2e3);e.unref(),x?.shutdown().catch(()=>void 0).finally(()=>{clearTimeout(e)})};process.once(`SIGTERM`,e),process.once(`SIGINT`,e)}return x}async function C(e){let t=await _();try{if(l())return await y(e);try{return await S().enqueue(e)}catch(t){let n=t instanceof Error?t.message:String(t);return i(`brain-writer`).debug({err:n},`Brain writer-thread unavailable — falling back to inline serialized executor`),await y(e)}}finally{await t()}}export{C as enqueueBrainWrite};