import { Action } from "@/contexts/signalR/signalr.provider";

import {
	CREATED_NOTIFICATION,
	DELETED_NOTIFICATION,
	UPDATED_NOTIFICATION,
} from "../enums/notifications.enum";

export const subscribeToNotifications = (
	subscribe: (method: string, callback: Action) => void,
	name: string,
	onCreated: Action,
	onDeleted?: Action,
	onUpdated?: Action,
) => {
	subscribe(`${name}.${CREATED_NOTIFICATION}`, onCreated);
	if (onDeleted) {
		subscribe(`${name}.${DELETED_NOTIFICATION}`, onDeleted);
	}
	if (onUpdated) {
		subscribe(`${name}.${UPDATED_NOTIFICATION}`, onUpdated);
	}
};

export const unsubscribeFromNotifications = (
	unsubscribe: (method: string, callback: Action) => void,
	name: string,
	onCreated: Action,
	onDeleted?: Action,
	onUpdated?: Action,
) => {
	unsubscribe(`${name}.${CREATED_NOTIFICATION}`, onCreated);
	if (onDeleted) {
		unsubscribe(`${name}.${DELETED_NOTIFICATION}`, onDeleted);
	}
	if (onUpdated) {
		unsubscribe(`${name}.${UPDATED_NOTIFICATION}`, onUpdated);
	}
};
