import { Injectable, inject, signal, computed } from '@angular/core'; import { NOTIFICATION_CONFIG } from '../providers/notification-config.provider'; import type { NtfNotification, NtfGroup } from '../types/notification.types'; import { groupByDate } from '../utils/time.utils'; @Injectable() export class NotificationService { private config = inject(NOTIFICATION_CONFIG); readonly notifications = signal([]); readonly unreadCount = computed(() => this.notifications().filter((n) => n.status === 'unread').length, ); readonly grouped = computed(() => { const items = this.notifications().filter((n) => n.status !== 'archived'); const dateGroups = groupByDate(items); return dateGroups.map((g) => ({ id: g.label.toLowerCase().replace(/\s+/g, '-'), label: g.label, notifications: g.items, })); }); add(notification: NtfNotification): void { this.notifications.update((list) => [notification, ...list]); } markRead(notificationId: string): void { this.notifications.update((list) => list.map((n) => (n.id === notificationId ? { ...n, status: 'read' as const } : n)), ); } markAllRead(): void { this.notifications.update((list) => list.map((n) => (n.status === 'unread' ? { ...n, status: 'read' as const } : n)), ); } archive(notificationId: string): void { this.notifications.update((list) => list.map((n) => (n.id === notificationId ? { ...n, status: 'archived' as const } : n)), ); } remove(notificationId: string): void { this.notifications.update((list) => list.filter((n) => n.id !== notificationId)); } clear(): void { this.notifications.set([]); } }