import{logger as e}from"../logger/index.mjs";import{getProjectById as t}from"./project.service.mjs";import{getDictionaryById as n,incrementVersion as r,updateDictionaryById as i}from"./dictionary.service.mjs";import{sendDictionaryUpdate as a}from"../controllers/eventListener.controller.mjs";import{mapDictionaryToAPI as o}from"../utils/mapper/dictionary.mjs";import{getUserById as s}from"./user.service.mjs";import{translationQueueName as c}from"./translationQueue.service.mjs";import{translateDictionaryDB as l}from"../utils/AI/translateDictionaryDB.mjs";import{getAIConfig as u}from"@intlayer/ai";import{Worker as d}from"bullmq";import f from"ioredis";import{defu as p}from"defu";const m=process.env.REDIS_URL;m||e.error(`REDIS_URL is not defined`);const h=new d(c,async c=>{let{dictionaryIds:d,targetLocales:f,projectId:m,userId:h}=c.data;e.info(`Processing translation job ${c.id} for project ${m}`);let g=await s(h),_=await t(m);if(!g||!_)throw Error(`User or Project not found`);let v=_.configuration?.ai,y=await u({userOptions:{},projectOptions:v,accessType:[`registered_user`,`apiKey`]},!0),b=d.length,x=0;for(let t of d)try{let s=await n(t),u=[...s.content.keys()??[]],d=u[u.length-1]||`v1`,m=s.content.get(d);if(!m){e.warn(`No content found for dictionary ${s.key}`);continue}let h=m.content,g=await l({content:h,sourceLocale:_.configuration?.internationalization?.defaultLocale||`en`,targetLocales:f,aiConfig:y,dictionaryDescription:s.description}),v=await n(t),S=v.content.get(d).content;for(let e of f)g[e]&&(S=p(g[e],S));let C=r(v),w=new Map(v.content);w.set(C,{content:S}),a([{dictionary:o(await i(t,{content:w})),status:`UPDATED`}]),x++,await c.updateProgress(x/b*100)}catch(n){e.error(`Error translating dictionary ${t}:`,n)}},{connection:new f(m,{maxRetriesPerRequest:null}),concurrency:5});h.on(`completed`,t=>{e.info(`Translation job ${t.id} completed`)}),h.on(`failed`,(t,n)=>{e.error(`Translation job ${t?.id} failed:`,n)});export{h as translationWorker};
//# sourceMappingURL=translationWorker.service.mjs.map