import{logger as e}from"../logger/index.mjs";import{formatResponse as t}from"../utils/responseData.mjs";import{ErrorHandler as n}from"../utils/errors/ErrorHandler.mjs";import{addTranslationJob as r,translationQueue as i}from"../services/translationQueue.service.mjs";const a=async(e,i)=>{let{project:a,user:o}=e.locals||{},{dictionaryIds:s,targetLocales:c}=e.body;if(!a)return n.handleGenericErrorResponse(i,`PROJECT_NOT_DEFINED`);if(!o)return n.handleGenericErrorResponse(i,`USER_NOT_DEFINED`);try{let e=t({data:{jobId:(await r({dictionaryIds:s,targetLocales:c,projectId:String(a.id),userId:String(o.id)})).id},message:`Translation started`});return i.send(e)}catch(e){return n.handleAppErrorResponse(i,e)}},o=async(t,n)=>{let{project:r,user:a}=t.locals||{};if(!a){n.raw.statusCode=401,n.raw.end();return}n.hijack();let o=n.getHeaders();Object.entries(o).forEach(([e,t])=>{t!==void 0&&n.raw.setHeader(e,t)}),Object.entries({"Content-Type":`text/event-stream; charset=utf-8`,"Cache-Control":`no-cache, no-transform`,Connection:`keep-alive`,"X-Accel-Buffering":`no`}).forEach(([e,t])=>{n.raw.setHeader(e,t)}),n.raw.flushHeaders?.(),n.raw.write(`: connected

`);let s=e=>{!n.raw.writableEnded&&!n.raw.destroyed&&n.raw.write(`data: ${JSON.stringify(e)}\n\n`)};try{let n=await(async()=>{let e=await i.getJobs([`active`,`waiting`,`delayed`,`completed`,`failed`]);return r?e.filter(e=>e.data.projectId===r.id):e.filter(e=>e.data.userId===a.id)})();for(let e of n){let t=await e.getState();s({jobId:e.id,state:t,progress:e.progress,data:e.data})}let o=setInterval(async()=>{try{let e=await i.getJobs([`active`,`waiting`,`delayed`]),t=r?e.filter(e=>e.data.projectId===r.id):e.filter(e=>e.data.userId===a.id);for(let e of t){let t=await e.getState();s({jobId:e.id,state:t,progress:e.progress})}}catch(t){e.error(`Error polling translation status`,t)}},2e3);t.raw.on(`close`,()=>{clearInterval(o)})}catch(t){e.error(`Error in translation status stream`,t),!n.raw.writableEnded&&!n.raw.destroyed&&(n.raw.write(`event: error\ndata: ${JSON.stringify({message:`Internal Server Error`})}\n\n`),n.raw.end())}};export{o as getTranslationStatus,a as translateDictionaries};
//# sourceMappingURL=translation.controller.mjs.map