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{getRedisClient as c}from"../utils/redis/connectRedis.mjs";import{translationQueueName as l}from"./translationQueue.service.mjs";import{translateDictionaryDB as u}from"../utils/AI/translateDictionaryDB.mjs";import{getAIConfig as d}from"@intlayer/ai";import{DefaultValues as f}from"@intlayer/config/client";import{Worker as p}from"bullmq";import{defu as m}from"defu";const h=async c=>{let{dictionaryIds:l,targetLocales:p,projectId:h,userId:g}=c.data;e.info(`Processing translation job ${c.id} for project ${h}`);let _=await s(g),v=await t(h);if(!_||!v)throw Error(`User or Project not found`);let y=v.configuration?.ai,b=await d({userOptions:{},projectOptions:y,accessType:[`registered_user`,`apiKey`]},!0),x=l.length,S=0;for(let t of l)try{let s=await n(t),l=[...s.content.keys()??[]],d=l[l.length-1]||`v1`,h=s.content.get(d);if(!h){e.warn(`No content found for dictionary ${s.key}`);continue}let g=h.content,_=await u({content:g,sourceLocale:v.configuration?.internationalization?.defaultLocale||f.Internationalization.DEFAULT_LOCALE,targetLocales:p,aiConfig:b,dictionaryDescription:s.description}),y=await n(t),C=y.content.get(d).content;for(let e of p)_[e]&&(C=m(_[e],C));let w=r(y),T=new Map(y.content);T.set(w,{content:C}),a([{dictionary:o(await i(t,{content:T})),status:`UPDATED`}]),S++,await c.updateProgress(S/x*100)}catch(n){e.error(`Error translating dictionary ${t}:`,n)}};let g=null;const _=()=>g||(g=new p(l,h,{connection:c(),concurrency:5}),g.on(`completed`,t=>{e.info(`Translation job ${t.id} completed`)}),g.on(`failed`,(t,n)=>{e.error(`Translation job ${t?.id} failed:`,n)}),g);export{_ as startTranslationWorker};
//# sourceMappingURL=translationWorker.service.mjs.map