import{getCompleteCatalogConfig as l}from"../../../plugins/catalog-entities/get-complete-catalog-config.js";import{parseAndValidateEntities as m,parseAndValidateEntity as d}from"../../../plugins/catalog-entities/entities/validate-entity.js";import{CatalogEntitiesService as u}from"../../../plugins/catalog-entities/database/catalog-entities-service.js";import{createPaginationParamsValidator as E}from"../../../providers/database/pagination/schemas.js";import{createEntityRelationDtoFromFileSchema as p}from"../../../plugins/catalog-entities/database/mappers/create-entity-relation-dto-from-file-schema.js";import{CacheService as f}from"../../../persistence/cache/services/cache-service.js";import{CATALOG_FILTERS_CACHE_NAMESPACE as j}from"../../../constants/plugins/catalog-entities.js";const w=["type","key","title","summary","tags","metadata","metadata.*","git","contact","links","id","source","sourceFile","createdAt","updatedAt"],y=async n=>{await(await f.getInstance({baseDbDir:n})).deleteByNamespace(j)},h=async(n,e)=>{const i=e.get("logger");try{const a=e.req.query(),r=E(w).parse(a),t=await n.getEntities(r);return e.json(t)}catch(a){return i.error(a),e.json({message:"Failed to get entities"},500)}},C=async(n,e)=>{const i=e.req.param("entityKey");if(!i)return e.json({message:"Entity key is required"},400);const a=await n.getEntityByKey(i);return a?e.json(a):e.json({message:"Entity not found"},404)},q=async(n,e,i,a)=>{const r=e.get("logger");try{const t=await e.req.json(),s=d(t,i),o=await n.createEntity(s),c=s.relations?.map(g=>p(s.key,g));return await n.createEntitiesRelations(c??[]),o?(y(a),e.json(o)):e.json({message:"Failed to create entity"},500)}catch(t){return r.error(t),t instanceof Error&&t.message.includes("validation failed")?e.json({message:t.message},400):e.json({message:"Failed to create entity"},500)}},F=async(n,e,i,a)=>{const r=e.get("logger");try{const t=await e.req.json(),s=m(t,i),o=await n.createEntities(s);return o.length?(y(a),e.json(o,207)):e.json({message:"Failed to create entities"},500)}catch(t){return r.error(t),t instanceof Error&&t.message.includes("validation failed")?e.json({message:t.message},400):e.json({message:"Failed to create entities"},500)}},v=async(n,e,i,a)=>{const r=e.get("logger"),t=e.req.param("entityKey");if(!t)return e.json({message:"Entity key is required"},400);try{const s=await e.req.json(),o=await n.updateEntityByKey(t,s);return o?(y(a),e.json(o)):e.json({message:"Failed to update entity"},500)}catch(s){return r.error(s),s instanceof Error&&s.message.includes("validation failed")?e.json({message:s.message},400):e.json({message:s.message},500)}},A=async(n,e,i,a)=>{const r=e.req.param("entityKey");return r?(await n.deleteEntityByKey(r),y(a),new Response(null,{status:204})):e.json({message:"Entity key is required"},400)},K={GET:h,POST:q,PUT:F},b={GET:C,DELETE:A,PATCH:v};function B(n){return async e=>{const i=await u.getInstance({baseDbDir:n.serverOutDir}),a=e.req.method,r=e.req.param("entityKey")?b:K,t=l(n.config.entitiesCatalog);return r[a]?await r[a](i,e,t,n.serverOutDir):e.json({message:"Method not allowed"},405)}}export{B as catalogHandler};
