"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerNginxConfigRoutes = registerNginxConfigRoutes;
const nginx_route_context_1 = require("./nginx-route.context");
function registerNginxConfigRoutes(context) {
    const { fastify, nginx, normalizeFsPath, ensureManageableConfigFile } = context;
    fastify.get('/config', async (_request, reply) => {
        try {
            const config = await nginx.config.readMainConfig();
            return reply.send({
                success: true,
                config,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/config', async (request, reply) => {
        const { content } = request.body;
        try {
            await nginx.config.writeMainConfig(content);
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.post('/config/validate', async (request, reply) => {
        const { content } = request.body;
        const result = await nginx.config.validateConfig(content);
        return reply.send(result);
    });
    fastify.get('/config/files', async (_request, reply) => {
        try {
            const files = await nginx.config.getIncludedConfigs();
            return reply.send({
                success: true,
                files,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.get('/config/file', async (request, reply) => {
        try {
            const filePath = await ensureManageableConfigFile(request.query?.filePath);
            const mainConfig = await nginx.config.readMainConfig();
            const isMainConfig = normalizeFsPath(mainConfig.mainConfigPath) === normalizeFsPath(filePath);
            if (isMainConfig) {
                return reply.send({
                    success: true,
                    config: mainConfig,
                });
            }
            const content = await nginx.config.readConfigFile(filePath);
            const isWritable = await nginx.config.isConfigFileWritable(filePath);
            return reply.send({
                success: true,
                config: {
                    mainConfigPath: filePath,
                    content,
                    isWritable,
                },
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
    fastify.put('/config/file', async (request, reply) => {
        try {
            const filePath = await ensureManageableConfigFile(request.body?.filePath);
            const content = request.body?.content ?? '';
            const mainConfig = await nginx.config.readMainConfig();
            const isMainConfig = normalizeFsPath(mainConfig.mainConfigPath) === normalizeFsPath(filePath);
            if (isMainConfig) {
                await nginx.config.writeMainConfig(content);
            }
            else {
                await nginx.config.writeConfigFile(filePath, content);
            }
            return reply.send({
                success: true,
            });
        }
        catch (error) {
            return (0, nginx_route_context_1.sendBadRequest)(reply, error);
        }
    });
}
