import{logger as e}from"../logger/index.mjs";import{DictionaryModel as t}from"../models/dictionary.model.mjs";import{getDemoDictionaries as n}from"../utils/demoDictionaries.mjs";import{ensureMongoDocumentToObject as r}from"../utils/ensureMongoDocumentToObject.mjs";import{GenericError as i}from"../utils/errors/ErrorsClass.mjs";import{removeObjectKeys as a}from"../utils/removeObjectKeys.mjs";import{validateDictionary as o}from"../utils/validation/validateDictionary.mjs";import{Types as s}from"mongoose";const c=async(n,r=0,i=100,a,o=!0)=>{try{return(await t.aggregate([{$match:n},...a&&Object.keys(a).length>0?[{$sort:a}]:[],{$skip:r},{$limit:i},...o?[]:[{$project:{content:0}}]])).map(e=>new t(e))}catch(t){throw e.error(`Error fetching dictionaries:`,t),t}},l=async e=>{let n=s.ObjectId.isValid(e)?new s.ObjectId(e):e,r=await t.aggregate([{$match:{_id:n}},{$addFields:{versions:{$map:{input:{$objectToArray:`$content`},as:`version`,in:`$$version.k`}}}}]);if(!r.length)throw new i(`DICTIONARY_NOT_FOUND`,{dictionaryId:e});return new t(r[0])},u=async(e,t)=>(await d([e],t))[0],d=async(e,n)=>{let r=await t.aggregate([{$match:{key:{$in:e},projectIds:n}},{$addFields:{versions:{$map:{input:{$objectToArray:`$content`},as:`version`,in:`$$version.k`}}}}]);if(!r)throw new i(`DICTIONARY_NOT_FOUND`,{dictionaryKeys:e,projectId:n});return r.map(e=>new t(e))},f=async(e,n)=>(await t.aggregate([{$match:{tags:{$in:e},projectIds:n}},{$addFields:{versions:{$map:{input:{$objectToArray:`$content`},as:`version`,in:`$$version.k`}}}}])).map(e=>new t(e)),p=async e=>{let n=await t.countDocuments(e);if(n===void 0)throw new i(`DICTIONARY_COUNT_FAILED`,{filters:e});return n},m=async e=>{let n=await o(e);if(Object.keys(n).length>0)throw new i(`DICTIONARY_INVALID_FIELDS`,{errors:n});return await t.create(e)},h=async(e,n)=>{let s=a(r(n),[`id`]),c=await o(s,Object.keys(s));if(Object.keys(c).length>0)throw new i(`DICTIONARY_INVALID_FIELDS`,{dictionaryId:e,errors:c});if((await t.updateOne({_id:e},s)).matchedCount===0)throw new i(`DICTIONARY_UPDATE_FAILED`,{dictionaryId:e});return await l(e)},g=async(e,n,o)=>{let s=await t.findOne({key:e,projectIds:o});if(!s)throw new i(`DICTIONARY_UPDATE_FAILED`,{dictionaryKey:e});let c=a(r(n),[`id`]);return Object.assign(s,c),await s.save(),s},_=async e=>{let n=await t.findByIdAndDelete(e);if(!n)throw new i(`DICTIONARY_NOT_FOUND`,{dictionaryId:e});return n},v=e=>{let t=e.match(/^v(\d+)$/);if(!t)throw Error(`Invalid version format: ${e}`);return parseInt(t[1],10)},y=e=>{let t=[...e.content.keys()??[]],n=t[t.length-1],r=v(n)+1,i=`v${r}`;for(;t.includes(i);)r+=1,i=`v${r}`;return i},b=async(e,t)=>{let r=n(e,t);for(let e of r)await m(e)};export{p as countDictionaries,b as createDemoDictionaries,m as createDictionary,_ as deleteDictionaryById,c as findDictionaries,d as getDictionariesByKeys,f as getDictionariesByTags,l as getDictionaryById,u as getDictionaryByKey,y as incrementVersion,h as updateDictionaryById,g as updateDictionaryByKey};
//# sourceMappingURL=dictionary.service.mjs.map