import { buildHealthEventNotificationCopy, healthEventNotificationType, shouldAttemptWechatNotification, shouldCreateWebNotification, } from './health-event-notification.mjs'; export function createHealthEventNotificationService({ createUserNotification = null, notificationDispatcher = null, logger = console, } = {}) { return { async notifyNewEvent(userId, event, { eventId = null } = {}) { const severity = String(event?.severity ?? 'info'); if (!shouldCreateWebNotification(severity)) { return { ok: true, skipped: true, reason: 'severity_silent', severity }; } const notificationType = healthEventNotificationType(severity); const { title, body } = buildHealthEventNotificationCopy(event); const data = { eventId, severity, ruleId: event.ruleId ?? null, eventType: event.eventType ?? null, metricType: event.metricType ?? null, }; let webCreated = false; if (typeof createUserNotification === 'function') { try { await createUserNotification({ userId, channel: 'web', notificationType, title, body, data, }); webCreated = true; } catch (error) { logger.warn?.('Health web notification failed:', error); } } let wechatSent = false; if ( shouldAttemptWechatNotification(severity) && typeof notificationDispatcher?.sendHealthEventNotification === 'function' ) { try { wechatSent = await notificationDispatcher.sendHealthEventNotification({ userId, title, body, eventId, severity, }); } catch (error) { logger.warn?.('Health wechat notification failed:', error); } } return { ok: true, skipped: false, webCreated, wechatSent, severity, eventId, }; }, }; }