function buildScopedWhere(base, scope) {
  const where = { ...base };
  const orgField = scope.orgField === null ? null : scope.orgField || "organizationId";
  const tenantField = scope.tenantField === null ? null : scope.tenantField || "tenantId";
  const softField = scope.softDeleteField === null ? null : scope.softDeleteField || "deletedAt";
  if (orgField) {
    if (scope.organizationIds !== void 0) {
      const ids = (scope.organizationIds ?? []).map((id) => typeof id === "string" ? id.trim() : id).filter((id) => typeof id === "string" && id.length > 0);
      if (ids.length === 0) {
        where[orgField] = { $in: [] };
      } else if (ids.length === 1) {
        where[orgField] = ids[0];
      } else {
        where[orgField] = { $in: ids };
      }
    } else if (scope.organizationId !== void 0) {
      where[orgField] = scope.organizationId;
    }
  }
  if (tenantField && scope.tenantId !== void 0) where[tenantField] = scope.tenantId;
  if (softField) where[softField] = null;
  return where;
}
function extractScopeFromAuth(auth) {
  if (!auth) return {};
  return { organizationId: auth.orgId ?? null, tenantId: auth.tenantId ?? null };
}
async function findOneScoped(em, entity, id, scope) {
  const orgField = scope.orgField || "organizationId";
  const tenantField = scope.tenantField || "tenantId";
  const where = { id };
  if (scope.organizationId != null) where[orgField] = scope.organizationId;
  if (scope.tenantId != null) where[tenantField] = scope.tenantId;
  return em.getRepository(entity).findOne(where);
}
async function softDelete(em, entity) {
  ;
  entity.deletedAt = /* @__PURE__ */ new Date();
  await em.persist(entity).flush();
}
export {
  buildScopedWhere,
  extractScopeFromAuth,
  findOneScoped,
  softDelete
};
//# sourceMappingURL=crud.js.map
