"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationSubscription = void 0;
const tslib_1 = require("tslib");
const graphql_yoga_1 = require("graphql-yoga");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const notification_1 = require("./notification");
let NotificationSubscription = class NotificationSubscription {
    notification(payload, subjects) {
        return payload.notification;
    }
};
exports.NotificationSubscription = NotificationSubscription;
tslib_1.__decorate([
    (0, type_graphql_1.Subscription)({
        subscribe: ({ args, context, info }) => {
            const { subjects = [] } = args;
            return (0, graphql_yoga_1.pipe)(shell_1.pubsub.subscribe('notification'), (0, graphql_yoga_1.filter)(async (payload) => {
                /* normally, subscription context doesn't have domain */
                const { domain, user } = context.state;
                const { notification: { subject, domain: pdomain } } = payload;
                const subdomain = pdomain === null || pdomain === void 0 ? void 0 : pdomain.subdomain;
                if (subdomain) {
                    if (domain === null || domain === void 0 ? void 0 : domain.subdomain) {
                        if (subdomain !== domain.subdomain) {
                            return false;
                        }
                    }
                    else if (!user.domains.find(d => d.subdomain === subdomain)) {
                        return false;
                    }
                }
                return !subject || subjects.indexOf(subject) !== -1;
            }));
        }
    }),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('subjects', type => [String], { nullable: true })),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object, Array]),
    tslib_1.__metadata("design:returntype", notification_1.Notification)
], NotificationSubscription.prototype, "notification", null);
exports.NotificationSubscription = NotificationSubscription = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(notification_1.Notification)
], NotificationSubscription);
//# sourceMappingURL=notification-subscription.js.map