"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initCollaborator = void 0;
var tslib_1 = require("tslib");
var isPepsicoEmail = function (email) {
    return typeof email === 'string' && email.toLowerCase().endsWith('@pepsico.com');
};
exports.initCollaborator = {
    rest: {
        method: 'GET',
        fullPath: '/api/pepsico-content/space-users/init-collaborator'
    },
    handler: function (ctx) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var user, space, spaceUsers, updated, collaborators, internalUsers, spaceUsers_1, spaceUsers_1_1, spaceUser, isCollaborator, e_1_1;
            var e_1, _a;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        ctx.getObject = this.getObject;
                        user = ctx.meta.user;
                        space = user === null || user === void 0 ? void 0 : user.spaceId;
                        if (!user || !user.is_space_admin) {
                            throw new Error('无权限!');
                        }
                        if (!space) {
                            throw new Error('无法获取当前工作区');
                        }
                        return [4, ctx.getObject('space_users').directFind({
                                filters: [['space', '=', space]],
                                fields: ['_id', 'email', 'is_collaborator']
                            })];
                    case 1:
                        spaceUsers = _b.sent();
                        updated = 0;
                        collaborators = 0;
                        internalUsers = 0;
                        _b.label = 2;
                    case 2:
                        _b.trys.push([2, 7, 8, 9]);
                        spaceUsers_1 = tslib_1.__values(spaceUsers), spaceUsers_1_1 = spaceUsers_1.next();
                        _b.label = 3;
                    case 3:
                        if (!!spaceUsers_1_1.done) return [3, 6];
                        spaceUser = spaceUsers_1_1.value;
                        isCollaborator = !isPepsicoEmail(spaceUser.email);
                        if (isCollaborator) {
                            collaborators++;
                        }
                        else {
                            internalUsers++;
                        }
                        if (!(spaceUser.is_collaborator !== isCollaborator)) return [3, 5];
                        return [4, ctx.getObject('space_users').directUpdate(spaceUser._id, {
                                is_collaborator: isCollaborator
                            })];
                    case 4:
                        _b.sent();
                        updated++;
                        _b.label = 5;
                    case 5:
                        spaceUsers_1_1 = spaceUsers_1.next();
                        return [3, 3];
                    case 6: return [3, 9];
                    case 7:
                        e_1_1 = _b.sent();
                        e_1 = { error: e_1_1 };
                        return [3, 9];
                    case 8:
                        try {
                            if (spaceUsers_1_1 && !spaceUsers_1_1.done && (_a = spaceUsers_1.return)) _a.call(spaceUsers_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7];
                    case 9: return [2, {
                            message: "\u521D\u59CB\u5316\u5B8C\u6210\uFF1A\u66F4\u65B0 ".concat(updated, " \u4E2A\u7528\u6237"),
                            total: spaceUsers.length,
                            updated: updated,
                            collaborators: collaborators,
                            internalUsers: internalUsers
                        }];
                }
            });
        });
    }
};
//# sourceMappingURL=initCollaborator.js.map