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