import { randomUUID } from 'node:crypto';
import { readJsonFile, writeJsonFile, ensureDir, getDataDir } from '../storage/json-store.js';
import { join } from 'node:path';
import { broadcastToWorkspace } from '../ws/connection-manager.js';
function notificationsPath(workspaceId) {
    const dir = join(getDataDir(), 'workspaces', workspaceId);
    ensureDir(dir);
    return join(dir, 'notifications.json');
}
function readAll(workspaceId) {
    return readJsonFile(notificationsPath(workspaceId)) ?? [];
}
function writeAll(workspaceId, notifications) {
    writeJsonFile(notificationsPath(workspaceId), notifications);
}
export function listNotifications(workspaceId) {
    return readAll(workspaceId).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
}
export function createNotification(workspaceId, type, title, description, data) {
    const notifications = readAll(workspaceId);
    const notification = {
        id: randomUUID(),
        workspaceId,
        type,
        title,
        description,
        data,
        read: false,
        createdAt: new Date().toISOString(),
    };
    notifications.push(notification);
    writeAll(workspaceId, notifications);
    broadcastToWorkspace(workspaceId, 'notification.created', notification);
    return notification;
}
export function markRead(workspaceId, notificationId) {
    const notifications = readAll(workspaceId);
    const n = notifications.find((n) => n.id === notificationId);
    if (!n)
        return null;
    n.read = true;
    writeAll(workspaceId, notifications);
    return n;
}
export function clearAll(workspaceId) {
    writeAll(workspaceId, []);
    broadcastToWorkspace(workspaceId, 'notification.cleared', null);
}
export function removeNotification(workspaceId, notificationId) {
    const all = readAll(workspaceId);
    const idx = all.findIndex((n) => n.id === notificationId);
    if (idx === -1)
        return null;
    const [removed] = all.splice(idx, 1);
    writeAll(workspaceId, all);
    return removed;
}
export function unreadCount(workspaceId) {
    return readAll(workspaceId).filter((n) => !n.read).length;
}
//# sourceMappingURL=notification-center.js.map