export type NotificationItem = {
	id: string;
	type: 'REPLY' | 'MENTION';
	documentId: string;
	referenceId: string;
	annotationId: string | null;
	read: boolean;
	createdAt: string;
	actorName: string;
	documentTitle: string | null;
};

export { relativeTime } from '$lib/shared/utils/time';

export function parseNotificationEvent(raw: string): NotificationItem | null {
	try {
		const parsed = JSON.parse(raw);
		if (
			typeof parsed.id !== 'string' ||
			typeof parsed.documentId !== 'string' ||
			typeof parsed.actorName !== 'string' ||
			!['REPLY', 'MENTION'].includes(parsed.type)
		) {
			console.warn('Unexpected SSE payload shape:', parsed);
			return null;
		}
		return parsed as NotificationItem;
	} catch {
		return null;
	}
}
