"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.user_router = void 0;
const express_1 = require("express");
const express_async_handler_1 = __importDefault(require("express-async-handler"));
function user_router(language, renderPage, _logger) {
    async function users(req, res, _next) {
        const users = await req.apiClient.listUsers();
        return renderPage('admin/users/index', language, res, req.user, { users });
    }
    async function createUser(req, res, _next) {
        const type = req.query.type || 'user';
        if (req.method === 'POST') {
            if (req.body.type === 'group') {
                req.body.users = req.body.users.map((user) => ({ url: user }));
            }
            console.log(req.body);
            const user = await req.apiClient.createUser({ username: req.body.username, password: req.body.password });
            res.redirect(303, '/' + language + '/admin/users/details?url=' + encodeURIComponent(user.url));
            return;
        }
        const users = type === 'group' ? (await req.apiClient.listUsers()) : undefined;
        const roles = []; //await req.apiClient.listRoles()
        return renderPage('admin/users/details', language, res, req.user, { users, roles, create: true, type });
    }
    async function userDetails(req, res, _next) {
        if (req.method === 'POST') {
            if (req.body.password === '') {
                delete req.body.password;
            }
            /*const user = await req.apiClient.updateUser(req.query.url as string, req.body)
            const userRoles = await req.apiClient.getRolesOfUser(user.id)
            const rolesToDelete = userRoles.filter((ur) => !req.body.roles.find((r: string) => r === ur.id))
            await req.apiClient.addRolesToUser(req.body.roles, user.id)
            await req.apiClient.removeRolesFromUser(rolesToDelete.map(r => r.id), user.id)*/
        }
        const user = await req.apiClient.getUser(req.query.url);
        //const userRoles: any = [];//await req.apiClient.getRolesOfUser(user.id)
        //const roles: any = [] //await req.apiClient.listRoles()
        const includedRoles = []; //roles.filter((r) => userRoles.find((ur) => ur.url === r.url))
        const excludedRoles = []; //roles.filter((r) => !userRoles.find((ur) => ur.url === r.url))
        return renderPage('admin/users/details', language, res, req.user, { user, includedRoles, excludedRoles });
    }
    async function userDelete(req, res, _next) {
        await req.apiClient.deleteUser(req.query.url);
        res.redirect(303, '/' + language + '/admin/users');
    }
    const router = (0, express_1.Router)();
    router.get('/', (0, express_async_handler_1.default)(users));
    router.get('/create', (0, express_async_handler_1.default)(createUser));
    router.post('/create', (0, express_async_handler_1.default)(createUser));
    router.get('/delete', (0, express_async_handler_1.default)(userDelete));
    router.get('/details', (0, express_async_handler_1.default)(userDetails));
    router.post('/details', (0, express_async_handler_1.default)(userDetails));
    return router;
}
exports.user_router = user_router;
//# sourceMappingURL=users.js.map