"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.notificationMiddleware = void 0;
const shell_1 = require("@things-factory/shell");
const fcm_1 = require("../controllers/fcm");
const debug = require('debug')('things-factory:notification:notification-middleware');
function notifier(context) {
    return async function ({ receivers, privileges, tokens, topic, subject = 'info', title, body, 
    // data,
    image, url, actions, timestamp = new Date(), mode = 'background' }) {
        const { domain, user } = context.state;
        try {
            if (mode === 'background') {
                /* for send webpush notification message */
                await (0, fcm_1.notify)({
                    receivers,
                    privileges,
                    tokens,
                    topic,
                    title,
                    body,
                    data: {
                        url,
                        timestamp
                    },
                    image,
                    actions
                });
            }
            else {
                /* for send publish notification to clients */
                // TODO CONFIRM data format
                await shell_1.pubsub.publish('notification', {
                    notification: {
                        domain,
                        subject,
                        title,
                        body,
                        image,
                        url,
                        timestamp
                    }
                });
            }
        }
        catch (err) {
            debug(err);
        }
    };
}
async function notificationMiddleware(context, next) {
    if (!context.state.notify) {
        context.state.notify = notifier(context);
    }
    await next();
}
exports.notificationMiddleware = notificationMiddleware;
//# sourceMappingURL=notification-middleware.js.map