import app from '@adonisjs/core/services/app';
import { AKEmailService } from '../email/email_service.js';
import { AKDeviceTokenType } from '../push_notifications/device_token.js';
const emailChannel = (eventTypesPreferenciablePerUser) => ({
    shouldProcess: (event) => !eventTypesPreferenciablePerUser.includes(event.type) ||
        event.recipient.enabledNotificationsEmail.includes(event.type),
    process: async (event) => {
        const emailService = await app.container.make('akago.email.service');
        const { subject, data, template } = await event.emailPayload();
        await emailService.send(AKEmailService.templatePath(template), event.recipient.email, subject, data, event.type);
    },
});
const pushNotificationChannel = (eventTypesPreferenciablePerUser) => ({
    shouldProcess: (event) => !eventTypesPreferenciablePerUser.includes(event.type) ||
        event.recipient.enabledNotificationsWeb.includes(event.type),
    process: async (event) => {
        const pushNotificationService = await app.container.make('akago.push_notification.service');
        const payload = await event.pushNotificationPayload();
        await pushNotificationService.send(event.recipient, payload.title, payload.body, {
            ...payload.data,
            type: event.type,
            relatedId: event.relatedId,
        }, AKDeviceTokenType.web);
    },
});
export const defaultChannels = (eventTypesPreferenciablePerUser) => ({
    email: emailChannel(eventTypesPreferenciablePerUser.email),
    pushNotification: pushNotificationChannel(eventTypesPreferenciablePerUser.pushNotification),
});
//# sourceMappingURL=channel.js.map