import { UniqueConstraintViolationException } from "@mikro-orm/core";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { CarrierWebhookProcessedEvent } from "../data/entities.js";
async function claimWebhookProcessing(em, idempotencyKey, providerKey, scope, eventType) {
  const record = em.create(CarrierWebhookProcessedEvent, {
    idempotencyKey,
    providerKey,
    eventType,
    organizationId: scope.organizationId,
    tenantId: scope.tenantId
  });
  try {
    await em.persist(record).flush();
    return true;
  } catch (error) {
    if (error instanceof UniqueConstraintViolationException) {
      return false;
    }
    throw error;
  }
}
async function releaseWebhookClaim(em, idempotencyKey, providerKey, scope) {
  const existing = await findOneWithDecryption(
    em,
    CarrierWebhookProcessedEvent,
    {
      idempotencyKey,
      providerKey,
      organizationId: scope.organizationId,
      tenantId: scope.tenantId
    },
    void 0,
    scope
  );
  if (!existing) return;
  await em.remove(existing).flush();
}
export {
  claimWebhookProcessing,
  releaseWebhookClaim
};
//# sourceMappingURL=webhook-utils.js.map
