"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = nodeVersionRoutes;
const errors_1 = require("@yinuo-ngm/errors");
async function nodeVersionRoutes(fastify) {
    fastify.get('/current', async () => {
        const service = fastify.core.nodeVersion;
        return await service.getCurrentVersion();
    });
    fastify.post('/switch', async (req) => {
        const { version } = req.body || {};
        if (!version) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.VERSION_REQUIRED, '请指定要切换的 Node 版本', {});
        }
        const service = fastify.core.nodeVersion;
        return await service.switchVersion(version);
    });
    fastify.post('/project-requirement', async (req) => {
        const { projectPath } = req.body || {};
        if (!projectPath) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.PROJECT_PATH_REQUIRED, '请指定项目路径', {});
        }
        const service = fastify.core.nodeVersion;
        return await service.detectProjectRequirement(projectPath);
    });
    fastify.post('/install', async (req) => {
        const { version } = req.body || {};
        if (!version) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.VERSION_REQUIRED, '请指定要安装的 Node 版本', {});
        }
        const service = fastify.core.nodeVersion;
        const result = await service.installNodeVersion(version);
        return result;
    });
    fastify.post('/uninstall', async (req) => {
        const { version } = req.body || {};
        if (!version) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.VERSION_REQUIRED, '请指定要卸载的 Node 版本', {});
        }
        const service = fastify.core.nodeVersion;
        const success = await service.uninstallNodeVersion(version);
        return { success };
    });
    fastify.post('/write-engine-config', async (req) => {
        const { projectPath, version } = req.body || {};
        if (!projectPath) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.PROJECT_PATH_REQUIRED, '请指定项目路径', {});
        }
        if (!version) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.VERSION_REQUIRED, '请指定版本要求', {});
        }
        const service = fastify.core.nodeVersion;
        const success = await service.writeEngineConfig(projectPath, version);
        return { success };
    });
}
