import{GenericError as e}from"../../utils/errors/ErrorsClass.mjs";import{ShowcaseProjectModel as t}from"../../models/showcaseProject.model.mjs";const n=async e=>{let{search:n,selectedUseCases:r,isOpenSource:i,page:a=1,pageSize:o=20}=e,s={};i&&(s.githubUrl={$exists:!0,$ne:null}),r&&r.length>0&&(s.tags={$in:r}),n?.trim()&&(s.$or=[{title:{$regex:n,$options:`i`}},{description:{$regex:n,$options:`i`}},{tags:{$regex:n,$options:`i`}}]);let c=await t.countDocuments(s),l=Math.ceil(c/o)||1;return{data:await t.aggregate([{$match:s},{$addFields:{score:{$subtract:[{$size:{$ifNull:[`$upvoters`,[]]}},{$size:{$ifNull:[`$downvoters`,[]]}}]}}},{$sort:{score:-1,createdAt:-1}},{$skip:(a-1)*o},{$limit:o}]),total_items:c,total_pages:l}},r=async n=>{let r=await t.findById(n).lean();if(!r)throw new e(`SHOWCASE_PROJECT_NOT_FOUND`,{projectId:n});return r},i=async e=>{let n;try{n=new URL(e).hostname}catch{return await t.findOne({websiteUrl:e}).lean()}let r=RegExp(`^https?://${n.replace(/\./g,`\\.`)}(/|$)`,`i`);return await t.findOne({websiteUrl:{$regex:r}}).lean()},a=async(e,n=4)=>await t.find({_id:{$ne:e}}).limit(n).lean(),o=async e=>{let n=new t({...e,...e,upvoters:[],downvoters:[]});return await n.save(),n},s=async(n,r)=>{let i=await t.findByIdAndUpdate(n,{$set:r},{new:!0}).lean();if(!i)throw new e(`SHOWCASE_PROJECT_NOT_FOUND`,{projectId:n});return i},c=async e=>{await t.findByIdAndDelete(e)},l=async(n,r,i)=>{let a=await t.findById(n);if(!a)throw new e(`SHOWCASE_PROJECT_NOT_FOUND`,{projectId:n});let o=a.upvoters||[],s=a.downvoters||[];if(i===`up`){let e=o.includes(r);return e?a.upvoters=o.filter(e=>e!==r):(a.upvoters.push(r),s.includes(r)&&(a.downvoters=s.filter(e=>e!==r))),await a.save(),{upvotes:a.upvoters.length,isUpVoted:!e,downvotes:a.downvoters.length,isDownVoted:!1}}else{let e=s.includes(r);return e?a.downvoters=s.filter(e=>e!==r):(a.downvoters.push(r),o.includes(r)&&(a.upvoters=o.filter(e=>e!==r))),await a.save(),{upvotes:a.upvoters.length,isUpVoted:!1,downvotes:a.downvoters.length,isDownVoted:!e}}},u=(e,t)=>l(e,t,`up`),d=(e,t)=>l(e,t,`down`);export{o as createShowcaseProject,c as deleteShowcaseProject,a as findOtherShowcaseProjects,r as findShowcaseProjectById,i as findShowcaseProjectByUrl,n as findShowcaseProjects,d as toggleShowcaseDownvote,u as toggleShowcaseUpvote,s as updateShowcaseProject};
//# sourceMappingURL=showcaseProject.service.mjs.map