import{CatalogEntitiesService as h}from"./database/catalog-entities-service.js";import{createPaginationParamsValidator as b}from"../../providers/database/pagination/schemas";import{parseSearch as E}from"../../providers/database/pagination/search";import{OPERATORS as F}from"../../providers/database/pagination/constants.js";import{CacheService as v}from"../../persistence/cache/services/cache-service.js";import{sha1 as A}from"../../utils/crypto.js";import{CATALOG_FILTERS_CACHE_NAMESPACE as w,CATALOG_FILTERS_CACHE_TTL_IN_SECONDS as I}from"../../constants/plugins/catalog-entities.js";const x={all:"all",domains:"domain",services:"service",teams:"team",users:"user","api-descriptions":"api-description","data-schemas":"data-schema"},T={team:{field:"type",operator:"equal",value:"user"},"api-description":{field:"type",operator:"equal",value:"api-operation"}},D=async({entitiesTypes:e,serverOutDir:a,catalogConfig:t,queries:n={}})=>{const l=await h.getInstance({baseDbDir:a}),c=M.concat("domains","owners"),o=b(c).parse(n),d=t.excludes?.map(s=>s.key)??[],p=e.filter(s=>s!=="all"),r=V(p,d);return o.filter?o.filter={op:F.AND,conditions:[o.filter,r]}:o.filter=r,await l.getEntitiesWithRelations({sort:[{field:"type",order:"ASC"}],limit:10,...o})},R=async(e,a,t)=>{const n=await h.getInstance({baseDbDir:a}),l=await n.getEntityWithRelationsByKey(e);if(!l)return null;const c=l.type==="data-schema"?await n.getRelatedEntities(e,{limit:1,filter:{field:"type",operator:"equal",value:"api-description"}}).then(p=>p.items?.[0]??null):null,o=T[l.type]??void 0,d=await n.getRelatedEntities(e,{limit:10,sort:[{field:"title",order:"ASC"}],filter:o,search:t?.search?E(t?.search,["key","type","title","summary"]):void 0});return{status:"success",entity:l,relatedEntity:c,relations:d}},_=e=>{const a=JSON.stringify({entitiesTypes:e.entitiesTypes.sort(),filtersConfig:e.filtersConfig});return A(a)},P=async({serverOutDir:e,entitiesTypes:a,filtersConfig:t})=>{if(!t||t.length===0)return{};const n=_({entitiesTypes:a,filtersConfig:t}),l=await v.getInstance({baseDbDir:e}),c=await l.get({key:n,namespace:w});if(c)return c;const o=await h.getInstance({baseDbDir:e}),d=[],p=new Map;for(const s of t)!s.options||s.options.length===0?d.push(s.property):p.set(s.property,s.options);const r=await o.getCatalogFilters({entitiesTypes:a,emptyFilters:d}),u={};for(const s of t){const g=p.get(s.property);if(!g){const i=r[s.property];u[s.property]=i&&i.length>0?i:[];continue}const m=(await o.getCatalogFilters({entitiesTypes:a,emptyFilters:[s.property]}))[s.property];if(!m){u[s.property]=[];continue}const f=new Map;for(const i of m){const y=i.value.toLowerCase().trim();f.set(y,{originalValue:i.value,count:i.count})}u[s.property]=g.map(i=>{const y=i.toLowerCase().trim(),S=f.get(y);return{value:S?.originalValue??i,count:S?.count??0}}).filter(i=>i.count>0).sort((i,y)=>i.value.localeCompare(y.value))}return await l.set({key:n,value:u,namespace:w,ttlInSeconds:I}),u},L=async(e,{props:a},t,{serverOutDir:n,getRouteSharedDataByFsPath:l})=>{const c=a?.catalogConfig||{};if(!e.params||!a?.catalogConfig)return{status:"notFound"};const[o,d,p]=e.params;if(!o)return{status:"notFound"};const r=N(c,o),u=r?.includes?.map(f=>f.type)??[];if(!r)return{status:"notFound"};if(!p&&d!=="entities"){if(r.hide)return{status:"notFound"};const f=await P({entitiesTypes:u,serverOutDir:n,filtersConfig:r.filters}),i=e.queries?.viewMode??"table";return{status:"success",catalogSwitcherItems:O(c,r),entitiesTypes:u,entities:await D({entitiesTypes:u,serverOutDir:n,catalogConfig:r,queries:e.queries}),catalogConfig:r,filters:f,initialViewMode:i}}const s=await R(p,n,e.queries),g=s?.entity.sourceFile,C=s?.entity.type==="api-description",m=g&&C&&l(g)||{};return s?{status:"success",entity:s.entity,relatedEntity:s.relatedEntity,relations:s.relations,entitiesCatalogConfig:c,catalogConfig:r,sharedDataIds:m}:{status:"notFound"}},N=(e,a)=>Object.values(e.catalogs??{}).find(t=>t&&typeof t=="object"&&"slug"in t&&t.slug===a),O=(e,a)=>Object.values(e.catalogs??{}).filter(t=>!t?.hide).map(t=>({labelTranslationKey:t?.catalogSwitcherLabelTranslationKey??t?.slug??"",slug:t?.slug??"",selected:t?.slug===a.slug})).sort((t,n)=>t.slug.localeCompare(n.slug)),V=(e,a)=>({op:"AND",conditions:[...e.length?[{field:"type",operator:"in",value:e}]:[],...a.length?[{field:"key",operator:"in",value:a,modifier:"not"}]:[]]}),M=["type","key","title","summary","tags","metadata","metadata.*","git","contact","links","id","source","sourceFile","createdAt","updatedAt"];var z=L;export{z as default};
