import C from"fs";import{promises as f}from"fs";import p from"path";var U=async(s={},w)=>{var d,y,L,h,b,S,v;let u=(d=s.mode)!=null?d:"memory",m=(y=s.logDir)!=null?y:".logs",l=(L=s.singleFile)!=null?L:!1,$={stdout:(b=(h=s.levels)==null?void 0:h.stdout)!=null?b:w?["debug","info","warn","error"]:["warn","error"],file:(v=(S=s.levels)==null?void 0:S.file)!=null?v:w?["info","debug","warn","error"]:["error"]};try{await f.mkdir(m,{recursive:!0})}catch(e){console.warn(`Failed to create log directory: ${e}`)}let i=new Map,a=new Map,F=e=>{let r=l?"all":e;if(!a.has(r)){let o=p.join(m,l?"combined.log":`${e}.log`);a.set(r,C.createWriteStream(o,{flags:"a"}))}return a.get(r)},g=(e,r,o)=>{let t={timestamp:new Date().toISOString(),level:r,message:o};$.stdout.includes(r)&&(r==="error"?console.error:console.log)(`[${e}] [${t.timestamp}] [${r.toUpperCase()}] ${t.message}`),$.file.includes(r)&&(u==="memory"?(i.has(e)||i.set(e,[]),i.get(e).push(t)):F(e).write(`[${t.timestamp}] [${r.toUpperCase()}] ${t.message}
`))};return{info:(e,r)=>g(e,"info",r),warn:(e,r)=>g(e,"warn",r),error:(e,r)=>g(e,"error",r),debug:(e,r)=>g(e,"debug",r),flush:async()=>{if(u==="memory"){let e=new Date().toISOString().replace(/:/g,"-"),r=Array.from(i.entries()).map(async([o,t])=>{try{let n=p.join(m,l?`combined-${e}.log`:`${o}-${e}.log`),P=t.map(c=>`[${c.timestamp}] [${c.level.toUpperCase()}] ${c.message}`);await f.mkdir(p.dirname(n),{recursive:!0}),await f.appendFile(n,P.join(`
`)+`
`)}catch(n){console.warn(`Failed to write log file for ${o}: ${n}`)}});await Promise.all(r)}for(let e of a.values())try{e.end()}catch(r){console.warn(`Failed to close log stream: ${r}`)}await new Promise(e=>setTimeout(e,10))}}};export{U as a};
