"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = svnRoutes;
const errors_1 = require("@yinuo-ngm/errors");
const node_path_1 = __importDefault(require("node:path"));
const env_1 = require("../env");
async function svnRoutes(fastify) {
    fastify.post("/sync/:projectId", async (req) => {
        const { projectId } = req.params;
        const p = await fastify.core.project.get(projectId);
        const assets = p.assets;
        if (!assets) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.ASSET_NOT_FOUND, 'Project assets are required for SVN sync');
            ;
        }
        const { iconsSvn, cutImageSvn } = assets;
        const svnSources = [iconsSvn, cutImageSvn].filter(s => s && s.kind === "svn");
        const results = [];
        for (const s of svnSources) {
            let localDir = s.localDir;
            if (!localDir) {
                localDir = node_path_1.default.join(env_1.env.dataDir, 'svn', projectId, s.label || s.id);
            }
            const r = await fastify.core.svnSync.sync(projectId, s.id, localDir, s.url);
            results.push(r);
        }
        return results;
    });
    fastify.get("/runtime/:projectId", async (req) => {
        const { projectId } = req.params;
        const runtimes = await fastify.core.svnSync.getRuntimeByProjectId(projectId);
        return runtimes;
    });
    fastify.post("/sync/stream/:projectId", async (req) => {
        const { projectId } = req.params;
        const assets = (await fastify.core.project.get(projectId)).assets;
        if (!assets) {
            throw new errors_1.CoreError(errors_1.CoreErrorCodes.ASSET_NOT_FOUND, 'Project assets are required for SVN sync');
            ;
        }
        const { iconsSvn, cutImageSvn } = assets;
        const svnSources = [iconsSvn, cutImageSvn].filter(s => s && s.kind === "svn");
        for (const s of svnSources) {
            let localDir = s.localDir;
            if (!localDir) {
                localDir = node_path_1.default.join(env_1.env.dataDir, 'svn', projectId, s.label || s.id);
            }
            await fastify.core.svnSync.syncWithStream(projectId, s.id, localDir, s.url);
        }
        return { message: "SVN sync started" };
    });
}
