"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessGroupSubscription = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const graphql_yoga_1 = require("graphql-yoga");
const shell_1 = require("@things-factory/shell");
const process_group_1 = require("./process-group");
let ProcessGroupSubscription = class ProcessGroupSubscription {
    processGroup(payload, id) {
        return payload.processGroup;
    }
};
exports.ProcessGroupSubscription = ProcessGroupSubscription;
tslib_1.__decorate([
    (0, type_graphql_1.Subscription)({
        subscribe: ({ args, context, info }) => {
            var _a;
            const { domain, user } = context.state;
            const { id } = args;
            const subdomain = domain === null || domain === void 0 ? void 0 : domain.subdomain;
            if (!domain) {
                throw new Error('domain required');
            }
            if (!((_a = user.domains) === null || _a === void 0 ? void 0 : _a.find(d => d.subdomain === subdomain))) {
                throw new Error(`domain(${subdomain}) is not working for user(${user.email}).`);
            }
            return (0, graphql_yoga_1.pipe)(shell_1.pubsub.subscribe('process-group'), (0, graphql_yoga_1.filter)((payload) => {
                const { id: processGroupId, domainId } = payload.processGroup;
                if (domainId !== domain.id) {
                    return false;
                }
                if (id !== processGroupId) {
                    return false;
                }
                return true;
            }));
        }
    }),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('id')),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object, String]),
    tslib_1.__metadata("design:returntype", process_group_1.ProcessGroup)
], ProcessGroupSubscription.prototype, "processGroup", null);
exports.ProcessGroupSubscription = ProcessGroupSubscription = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_group_1.ProcessGroup)
], ProcessGroupSubscription);
//# sourceMappingURL=process-group-subscription.js.map