"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessSubscription = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const graphql_subscriptions_1 = require("graphql-subscriptions");
const shell_1 = require("@things-factory/shell");
const process_1 = require("./process");
let ProcessSubscription = class ProcessSubscription {
    process(payload, id) {
        return payload.process;
    }
};
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_subscriptions_1.withFilter)(() => shell_1.pubsub.asyncIterator('process'), (payload, variables, context, info) => {
                const { domainId, id: processId } = payload.process;
                if (domainId !== domain.id) {
                    return false;
                }
                if (id !== processId) {
                    return false;
                }
                return true;
            })(_, args, context, info);
        }
    }),
    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_1.Process)
], ProcessSubscription.prototype, "process", null);
ProcessSubscription = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_1.Process)
], ProcessSubscription);
exports.ProcessSubscription = ProcessSubscription;
//# sourceMappingURL=process-subscription.js.map