"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getReviewerAgentsInfo = void 0;
var tslib_1 = require("tslib");
var getAgent = function (ctx, spaceId, fromId) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var now, records, userId;
    var _a;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                now = new Date();
                return [4, ctx.getObject("process_delegation_rules").find({
                        filters: [
                            ["space", "=", spaceId],
                            ["from", "=", fromId],
                            ["enabled", "=", true],
                            ["start_time", "<=", now],
                            ["end_time", ">=", now]
                        ],
                        top: 1,
                        sort: "modified desc"
                    })];
            case 1:
                records = _b.sent();
                userId = records.length > 0 ? (_a = records[0]) === null || _a === void 0 ? void 0 : _a.to : null;
                console.log("getAgent", userId, JSON.stringify({
                    filters: [
                        ["space", "=", spaceId],
                        ["from", "=", fromId],
                        ["enabled", "=", true],
                        ["start_time", "<=", now],
                        ["end_time", ">=", now]
                    ],
                    top: 1,
                    sort: "modified desc"
                }));
                return [2, userId];
        }
    });
}); };
var getReviewerAgentsInfo = function (ctx, spaceId, reviewers) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var agentInfo, reviewers_1, reviewers_1_1, reviewer, agent, e_1_1;
    var e_1, _a;
    return tslib_1.__generator(this, function (_b) {
        switch (_b.label) {
            case 0:
                agentInfo = {
                    userIds: [],
                    clientUserIds: []
                };
                _b.label = 1;
            case 1:
                _b.trys.push([1, 6, 7, 8]);
                reviewers_1 = tslib_1.__values(reviewers), reviewers_1_1 = reviewers_1.next();
                _b.label = 2;
            case 2:
                if (!!reviewers_1_1.done) return [3, 5];
                reviewer = reviewers_1_1.value;
                return [4, getAgent(ctx, spaceId, reviewer)];
            case 3:
                agent = _b.sent();
                agentInfo.userIds.push(agent ? agent : reviewer);
                agentInfo.clientUserIds.push(reviewer);
                _b.label = 4;
            case 4:
                reviewers_1_1 = reviewers_1.next();
                return [3, 2];
            case 5: return [3, 8];
            case 6:
                e_1_1 = _b.sent();
                e_1 = { error: e_1_1 };
                return [3, 8];
            case 7:
                try {
                    if (reviewers_1_1 && !reviewers_1_1.done && (_a = reviewers_1.return)) _a.call(reviewers_1);
                }
                finally { if (e_1) throw e_1.error; }
                return [7];
            case 8:
                console.log("getReviewerAgentsInfo:", reviewers, agentInfo);
                return [2, agentInfo];
        }
    });
}); };
exports.getReviewerAgentsInfo = getReviewerAgentsInfo;
//# sourceMappingURL=agent.js.map