"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.device_router = void 0;
const express_1 = require("express");
const express_async_handler_1 = __importDefault(require("express-async-handler"));
function device_router(language, renderPage, _logger) {
    async function devices(req, res, _next) {
        const devices = await req.apiClient.listDevices();
        return renderPage('admin/devices/index', language, res, req.user, { devices });
    }
    async function createDevice(req, res, _next) {
        const type = req.query.type || 'device';
        if (req.method === 'POST') {
            if (req.body.type === 'group') {
                req.body.devices = req.body.devices?.map((device) => ({ url: device })) ?? [];
            }
            if (req.body.services === '') {
                delete req.body.services;
            }
            req.body.isPublic = req.body.isPublic === 'Yes';
            console.log(req.body);
            try {
                const device = await req.apiClient.createDevice({ ...req.body, isPublic: true });
                res.redirect(303, '/' + language + '/admin/devices/details?url=' + encodeURIComponent(device.url));
            }
            catch (e) {
                console.log(e);
            }
            return;
        }
        const devices = type === 'group' ? (await req.apiClient.listDevices()) : undefined;
        return renderPage('admin/devices/details', language, res, req.user, { devices, create: true, type });
    }
    async function deviceDetails(req, res, _next) {
        if (req.method === 'POST') {
            if (req.body.type === 'group') {
                req.body.devices = req.body.devices?.map((device) => ({ url: device })) ?? [];
            }
            if (req.body.type === 'device') {
                req.body.services = JSON.parse(req.body.services);
            }
            req.body.isPublic = req.body.isPublic === 'Yes';
            await req.apiClient.updateDevice(req.query.url, req.body);
        }
        const device = await req.apiClient.getDevice(req.query.url);
        let params = {};
        if (device.type === 'group') {
            const devices = (await req.apiClient.listDevices());
            const includedDevices = devices.filter((d) => device.devices.find((dd) => dd.url === d.url));
            const excludedDevices = devices.filter((d) => !device.devices.find((dd) => dd.url === d.url));
            params = { includedDevices, excludedDevices };
        }
        return renderPage('admin/devices/details', language, res, req.user, { device, type: device.type, ...params });
    }
    async function deviceDelete(req, res, _next) {
        await req.apiClient.deleteDevice(req.query.url);
        res.redirect(303, '/' + language + '/admin/devices');
    }
    async function deviceToken(req, res, _next) {
        const url = req.query.url;
        const token = req.user?.username ? await req.apiClient.createToken({
            username: req.user?.username,
            claims: { device_token: true },
        }) : "";
        return renderPage('admin/devices/token', language, res, req.user, { token, url });
    }
    const router = (0, express_1.Router)();
    router.get('/', (0, express_async_handler_1.default)(devices));
    router.get('/create', (0, express_async_handler_1.default)(createDevice));
    router.post('/create', (0, express_async_handler_1.default)(createDevice));
    router.get('/delete', (0, express_async_handler_1.default)(deviceDelete));
    router.get('/details', (0, express_async_handler_1.default)(deviceDetails));
    router.post('/details', (0, express_async_handler_1.default)(deviceDetails));
    router.post('/token', (0, express_async_handler_1.default)(deviceToken));
    return router;
}
exports.device_router = device_router;
//# sourceMappingURL=devices.js.map