import{and as a,count as h,eq as l,isNotNull as R,or as A,sql as u}from"drizzle-orm";import{applyPagination as b}from"../../../../../providers/database/pagination/index.js";import{getEffectivePaginationLimit as D}from"../../../../../providers/database/pagination/limit.js";import{entitiesTable as t}from"../../../../../providers/database/databases/sqlite-db/schemas/entities-table.js";import{entitiesAttributesTable as m}from"../../../../../providers/database/databases/sqlite-db/schemas/entities-attributes-table.js";import{VERSION_NOT_SPECIFIED as L}from"@redocly/theme/core/constants";import{applyFilter as B}from"../../../../../providers/database/pagination/filter.js";import{getFirstRunRow as O}from"../../../../../providers/database/utils/get-first-row.js";import{createEntityReadModel as f}from"../../mappers/create-entity-read-model.js";import{createEntityFieldsForSelect as w,FIELDS_TO_SELECT_FOR_ENTITY as p}from"../utils.js";import{buildRbacFilter as g}from"../utils/build-rbac-filter.js";import{buildEntitiesExclusionFilter as F}from"../utils/build-entities-exclusion-filter.js";class Y{#t;constructor(e){this.#t=e}async getEntities({paginationParams:e,rbacTeams:i,excludedTypes:n,excludedEntities:o}){const s=F(n,o,"entities"),r=this.#t.client.select(w("entities")).from(t).leftJoin(m,l(t.key,m.entityKey)).where(a(g(i,"entities_attributes"),s)),c=this.#t.client.select(p).from(r.as("combined_entities")),S=this.#t.client.select(p).from(r.as("combined_entities")).$dynamic(),T=b(S,{...e,limit:void 0,skip:void 0,after:void 0,before:void 0}),C=this.#t.client.$count(T),_=c.$dynamic(),y=D(e),$=b(_,{...e,limit:y+1}),[k,I]=await Promise.all([$.all(),C]),E=k,N=E.length>y;return{items:E.slice(0,y).map(d=>f(d)).filter(d=>d!==null),hasMore:N,total:I}}async getEntityById(e,i){const{rbacTeams:n,excludedTypes:o,excludedEntities:s}=i||{},r=F(o,s,"entities"),c=await this.#t.client.select(w("entities")).from(t).leftJoin(m,l(t.key,m.entityKey)).where(a(l(t.id,e),g(n,"entities_attributes"),r)).get();return c?f(c):null}async getEntityKeysAndVersionsBySourceFile(e){const i=await this.#t.client.selectDistinct({keyVersion:u`${t.key} || ':' || COALESCE(${t.version}, ${L})`.as("keyVersion")}).from(t).where(a(l(t.sourceFile,e),l(t.source,"file"),R(t.key))).all();return new Set(i.map(n=>n.keyVersion))}async getEntitiesCountByTypes(){return this.#t.client.select({type:t.type,count:h()}).from(t).where(a(l(t.isCurrent,!0),l(t.isDeleted,!1))).groupBy(t.type)}async getOutdatedEntities(e){const i=this.#t.client.select(p).from(t).$dynamic(),{whereCondition:n}=B(i,e),o=A(u`scorecards_status = 'OUTDATED'`,u`scorecards_status IS NULL`),s=n?a(n,o):o;return(await i.where(s).all()).map(c=>f(c)).filter(c=>c!==null)}async getEntitiesCount(e,i,n){const o=await this.#t.client.select({count:h()}).from(t).where(l(t.source,e));let s=Number(o[0]?.count??0);if(i?.length){const r=await this.#e(e,i);s+=i.length-r}return n!==void 0&&(s-=n),{total:s}}async#e(e,i){const n=u.join(i.map(r=>u`(${r.key}, ${r.version}, ${r.revision})`),u`, `),o=await this.#t.client.run(u`SELECT count(*) AS count FROM entities WHERE source = ${e} AND (key, version, revision) IN (VALUES ${n})`),s=O(o);return Number(s?.count??0)}}export{Y as EntitiesReadRepository};
