"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const config_1 = require("../config");
const memory_prompts_1 = require("../memory-prompts");
const agent_prompts_1 = require("../agent-prompts");
const router = (0, express_1.Router)();
/** Same permission gate as `routes/agent-prompts.ts` — admin, owner, or a
 *  share with `edit` permission may toggle the project's instructions file. */
function resolveProject(projectId, username, res) {
    const project = (0, config_1.getProject)(projectId);
    if (!project) {
        res.status(404).json({ error: 'Project not found' });
        return null;
    }
    if (!(0, config_1.isAdminUser)(username) &&
        !(0, config_1.isProjectOwner)(project, username) &&
        !project.shares?.some((s) => s.username === username && s.permission === 'edit')) {
        res.status(403).json({ error: 'Access denied' });
        return null;
    }
    return { folderPath: project.folderPath, cliTool: project.cliTool };
}
// GET /api/memory/project/:projectId
// → { items, claudeMdLineCount, instructionsFilename }
router.get('/project/:projectId', (req, res) => {
    const p = resolveProject(req.params.projectId, req.user?.username || '', res);
    if (!p)
        return;
    const result = (0, memory_prompts_1.listMemoryPrompts)(p.folderPath, p.cliTool);
    res.json({ ...result, instructionsFilename: (0, agent_prompts_1.instructionsFilename)(p.cliTool) });
});
// POST /api/memory/project/:projectId/toggle  body: { filename, action }
router.post('/project/:projectId/toggle', (req, res) => {
    const p = resolveProject(req.params.projectId, req.user?.username || '', res);
    if (!p)
        return;
    const { filename, action } = (req.body ?? {});
    if (typeof filename !== 'string' || !filename) {
        res.status(400).json({ error: 'filename is required' });
        return;
    }
    if (action !== 'insert' && action !== 'remove') {
        res.status(400).json({ error: 'action must be "insert" or "remove"' });
        return;
    }
    const result = (0, memory_prompts_1.toggleMemoryPrompt)(p.folderPath, filename, action, p.cliTool);
    res.json({ ...result, instructionsFilename: (0, agent_prompts_1.instructionsFilename)(p.cliTool) });
});
exports.default = router;
//# sourceMappingURL=memory-prompts.js.map