import { warnOnEncryptedLikeFilter } from "./likeFilterWarning.js";
import { decryptEntitiesWithFallbackScope } from "./subscriber.js";
async function findWithDecryption(em, entityName, where, options, scope) {
  await warnOnEncryptedLikeFilter({
    em,
    entityName,
    where,
    tenantId: scope?.tenantId,
    encryptionService: scope?.encryptionService
  });
  const records = await em.find(entityName, where, options);
  if (!Array.isArray(records) || records.length === 0) return records ?? [];
  await decryptEntitiesWithFallbackScope(records, {
    em,
    tenantId: scope?.tenantId ?? null,
    organizationId: scope?.organizationId ?? null,
    encryptionService: scope?.encryptionService ?? null
  });
  return records;
}
async function findOneWithDecryption(em, entityName, where, options, scope) {
  await warnOnEncryptedLikeFilter({
    em,
    entityName,
    where,
    tenantId: scope?.tenantId,
    encryptionService: scope?.encryptionService
  });
  const record = await em.findOne(entityName, where, options);
  if (!record) return record;
  await decryptEntitiesWithFallbackScope(record, {
    em,
    tenantId: scope?.tenantId ?? null,
    organizationId: scope?.organizationId ?? null,
    encryptionService: scope?.encryptionService ?? null
  });
  return record;
}
async function findAndCountWithDecryption(em, entityName, where, options, scope) {
  await warnOnEncryptedLikeFilter({
    em,
    entityName,
    where,
    tenantId: scope?.tenantId,
    encryptionService: scope?.encryptionService
  });
  const [recordsRaw, count] = await em.findAndCount(
    entityName,
    where,
    options
  );
  const records = Array.isArray(recordsRaw) ? recordsRaw : [];
  if (!records.length) return [records, count];
  await decryptEntitiesWithFallbackScope(records, {
    em,
    tenantId: scope?.tenantId ?? null,
    organizationId: scope?.organizationId ?? null,
    encryptionService: scope?.encryptionService ?? null
  });
  return [records, count];
}
export {
  findAndCountWithDecryption,
  findOneWithDecryption,
  findWithDecryption
};
//# sourceMappingURL=find.js.map
