"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationResolver = void 0;
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const graphql_subscriptions_1 = require("graphql-subscriptions");
const notification_1 = require("./notification");
const debug = require('debug')('things-factory:notification:notification-subscription');
let NotificationResolver = class NotificationResolver {
    notification(payload, subject) {
        return payload.notification;
    }
};
__decorate([
    (0, type_graphql_1.Subscription)({
        subscribe: (0, graphql_subscriptions_1.withFilter)(() => shell_1.pubsub.asyncIterator('notification'), (payload, variables, context, info) => {
            /* normally, subscription context doesn't have domain */
            const { domain, user } = context.state;
            const { subjects = [] } = variables;
            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;
        })
    }),
    __param(0, (0, type_graphql_1.Root)()),
    __param(1, (0, type_graphql_1.Arg)('subjects', type => [String], { nullable: true })),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Array]),
    __metadata("design:returntype", notification_1.Notification)
], NotificationResolver.prototype, "notification", null);
NotificationResolver = __decorate([
    (0, type_graphql_1.Resolver)()
], NotificationResolver);
exports.NotificationResolver = NotificationResolver;
//# sourceMappingURL=notification-resolver.js.map