import { Notification } from "../data/entities.js";
import { NOTIFICATION_EVENTS } from "./events.js";
import { assertSafeNotificationHref, sanitizeNotificationActions } from "./safeHref.js";
function buildNotificationEntity(em, input, recipientUserId, ctx) {
  const actions = sanitizeNotificationActions(input.actions);
  const linkHref = assertSafeNotificationHref(input.linkHref);
  return em.create(Notification, {
    recipientUserId,
    type: input.type,
    // i18n-first: store keys and variables for translation at display time
    titleKey: input.titleKey,
    bodyKey: input.bodyKey,
    titleVariables: input.titleVariables,
    bodyVariables: input.bodyVariables,
    // Fallback text (required for backward compatibility)
    title: input.title || input.titleKey || "",
    body: input.body,
    icon: input.icon,
    severity: input.severity ?? "info",
    actionData: actions ? {
      actions,
      primaryActionId: input.primaryActionId
    } : null,
    sourceModule: input.sourceModule,
    sourceEntityType: input.sourceEntityType,
    sourceEntityId: input.sourceEntityId,
    linkHref,
    groupKey: input.groupKey,
    expiresAt: input.expiresAt ? new Date(input.expiresAt) : null,
    tenantId: ctx.tenantId,
    organizationId: ctx.organizationId
  });
}
async function emitNotificationCreated(eventBus, notification, ctx) {
  await eventBus.emit(NOTIFICATION_EVENTS.CREATED, {
    notificationId: notification.id,
    recipientUserId: notification.recipientUserId,
    type: notification.type,
    title: notification.title,
    tenantId: ctx.tenantId,
    organizationId: ctx.organizationId
  });
}
async function emitNotificationCreatedBatch(eventBus, notifications, ctx) {
  for (const notification of notifications) {
    await emitNotificationCreated(eventBus, notification, ctx);
  }
}
export {
  buildNotificationEntity,
  emitNotificationCreated,
  emitNotificationCreatedBatch
};
//# sourceMappingURL=notificationFactory.js.map
