"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCodeRouter = createCodeRouter;
const express_1 = require("express");
const validation_1 = require("../../api/rest/validation");
/** Express 5 wildcard params are arrays — join them back into a path string. */
function joinParam(value) {
    return Array.isArray(value) ? value.join('/') : String(value);
}
function createCodeRouter() {
    const router = (0, express_1.Router)({ mergeParams: true });
    function getProject(req) {
        return req.project;
    }
    // List code files
    router.get('/files', (0, validation_1.validateQuery)(validation_1.listQuerySchema), (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.codeManager)
                return res.json({ results: [] });
            const q = req.validatedQuery;
            const { results: files, total } = p.codeManager.listFiles(q.filter, q.limit, q.offset);
            res.json({ results: files, total });
        }
        catch (err) {
            next(err);
        }
    });
    // Get symbols for a file
    router.get('/files/*fileId/symbols', (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.codeManager)
                return res.status(404).json({ error: 'No code graph' });
            const fileId = joinParam(req.params.fileId);
            const symbols = p.codeManager.getFileSymbols(fileId);
            res.json({ results: symbols });
        }
        catch (err) {
            next(err);
        }
    });
    // Get edges for a symbol (imports, contains, extends, implements)
    // Must be registered before /symbols/*symbolId to avoid wildcard consuming "edges"
    router.get('/symbols/*symbolId/edges', (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.codeManager)
                return res.status(404).json({ error: 'No code graph' });
            const symbolId = joinParam(req.params.symbolId);
            const edges = p.codeManager.getSymbolEdges(symbolId);
            res.json({ results: edges });
        }
        catch (err) {
            next(err);
        }
    });
    // Get symbol by ID
    router.get('/symbols/*symbolId', (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.codeManager)
                return res.status(404).json({ error: 'No code graph' });
            const symbolId = joinParam(req.params.symbolId);
            const symbol = p.codeManager.getSymbol(symbolId);
            if (!symbol)
                return res.status(404).json({ error: 'Symbol not found' });
            const { embedding: _, fileEmbedding: _fe, ...rest } = symbol;
            res.json(rest);
        }
        catch (err) {
            next(err);
        }
    });
    // Search code
    router.get('/search', (0, validation_1.validateQuery)(validation_1.searchQuerySchema), async (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.codeManager)
                return res.json({ results: [] });
            const q = req.validatedQuery;
            const results = await p.codeManager.search(q.q, {
                topK: q.topK,
                minScore: q.minScore,
                searchMode: q.searchMode,
                bfsDepth: q.bfsDepth,
                maxResults: q.maxResults,
                bfsDecay: q.bfsDecay,
                includeBody: q.includeBody,
            });
            res.json({ results });
        }
        catch (err) {
            next(err);
        }
    });
    return router;
}
