import {
  findOneWithDecryption
} from "@open-mercato/shared/lib/encryption/find";
import { AiAgentMutationPolicyOverride } from "../entities.js";
class AiAgentMutationPolicyOverrideRepository {
  constructor(em) {
    this.em = em;
  }
  async get(agentId, ctx) {
    if (!agentId || !ctx?.tenantId) return null;
    const row = await findOneWithDecryption(
      this.em,
      AiAgentMutationPolicyOverride,
      {
        tenantId: ctx.tenantId,
        organizationId: ctx.organizationId ?? null,
        agentId
      },
      {},
      { tenantId: ctx.tenantId ?? null, organizationId: ctx.organizationId ?? null }
    );
    return row ?? null;
  }
  async set(input, ctx) {
    if (!ctx?.tenantId) {
      throw new Error("AiAgentMutationPolicyOverrideRepository.set requires tenantId");
    }
    if (!input?.agentId) {
      throw new Error("AiAgentMutationPolicyOverrideRepository.set requires agentId");
    }
    const normalized = normalizeNotes(input.notes);
    return this.em.transactional(async (tx) => {
      const existing = await findOneWithDecryption(
        tx,
        AiAgentMutationPolicyOverride,
        {
          tenantId: ctx.tenantId,
          organizationId: ctx.organizationId ?? null,
          agentId: input.agentId
        },
        {},
        { tenantId: ctx.tenantId ?? null, organizationId: ctx.organizationId ?? null }
      );
      if (existing) {
        existing.mutationPolicy = input.mutationPolicy;
        existing.notes = normalized;
        existing.createdByUserId = ctx.userId ?? existing.createdByUserId ?? null;
        existing.updatedAt = /* @__PURE__ */ new Date();
        await tx.persist(existing).flush();
        return existing;
      }
      const row = tx.create(AiAgentMutationPolicyOverride, {
        tenantId: ctx.tenantId,
        organizationId: ctx.organizationId ?? null,
        agentId: input.agentId,
        mutationPolicy: input.mutationPolicy,
        notes: normalized,
        createdByUserId: ctx.userId ?? null
      });
      await tx.persist(row).flush();
      return row;
    });
  }
  async clear(agentId, ctx) {
    if (!agentId || !ctx?.tenantId) return false;
    return this.em.transactional(async (tx) => {
      const existing = await findOneWithDecryption(
        tx,
        AiAgentMutationPolicyOverride,
        {
          tenantId: ctx.tenantId,
          organizationId: ctx.organizationId ?? null,
          agentId
        },
        {},
        { tenantId: ctx.tenantId ?? null, organizationId: ctx.organizationId ?? null }
      );
      if (!existing) return false;
      await tx.remove(existing).flush();
      return true;
    });
  }
}
function normalizeNotes(notes) {
  if (typeof notes !== "string") return null;
  const trimmed = notes.trim();
  if (!trimmed) return null;
  return notes;
}
var AiAgentMutationPolicyOverrideRepository_default = AiAgentMutationPolicyOverrideRepository;
export {
  AiAgentMutationPolicyOverrideRepository,
  AiAgentMutationPolicyOverrideRepository_default as default
};
//# sourceMappingURL=AiAgentMutationPolicyOverrideRepository.js.map
