// @crash-shield-ignore-file — config/cache reader for Planu-controlled JSON; writer is this codebase, shape guaranteed by build/seed.
// Planu — Memory config generator engine (SPEC-375)
// Generates .claude.json MCP entries for mem0/OpenMemory
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
async function readClaudeJson(configPath) {
    try {
        const content = await readFile(configPath, 'utf-8');
        return JSON.parse(content);
    }
    catch {
        return {};
    }
}
async function writeClaudeJson(configPath, config) {
    await writeFile(configPath, JSON.stringify(config, null, 2) + '\n', 'utf-8');
}
export function resolveClaudeJsonPath(projectPath) {
    return join(projectPath, '.claude.json');
}
export function detectMemoryProvider(config) {
    const servers = config.mcpServers;
    if (servers === undefined) {
        return 'none';
    }
    const memoryServer = servers.memory;
    if (memoryServer === undefined) {
        return 'none';
    }
    const args = memoryServer.args;
    if (!args.includes('@mem0/mcp')) {
        return 'openmemory';
    }
    const apiKey = memoryServer.env?.MEM0_API_KEY;
    if (apiKey !== undefined && apiKey !== '') {
        return 'mem0-cloud';
    }
    return 'openmemory';
}
export async function getMemoryStatus(projectPath) {
    const configPath = resolveClaudeJsonPath(projectPath);
    const config = await readClaudeJson(configPath);
    const provider = detectMemoryProvider(config);
    return {
        provider,
        connected: false,
        memoryCount: 0,
        configPath,
    };
}
export async function setupOpenMemory(projectPath) {
    const configPath = resolveClaudeJsonPath(projectPath);
    const config = await readClaudeJson(configPath);
    const memoryServer = {
        command: 'npx',
        args: ['-y', '@mem0/mcp'],
    };
    const updated = {
        ...config,
        mcpServers: {
            ...(config.mcpServers ?? {}),
            memory: memoryServer,
        },
    };
    await writeClaudeJson(configPath, updated);
    return configPath;
}
export async function setupMem0Cloud(projectPath, apiKey) {
    const configPath = resolveClaudeJsonPath(projectPath);
    const config = await readClaudeJson(configPath);
    const memoryServer = {
        command: 'npx',
        args: ['-y', '@mem0/mcp'],
        env: { MEM0_API_KEY: apiKey },
    };
    const updated = {
        ...config,
        mcpServers: {
            ...(config.mcpServers ?? {}),
            memory: memoryServer,
        },
    };
    await writeClaudeJson(configPath, updated);
    return configPath;
}
export async function resetMemoryConfig(projectPath) {
    const configPath = resolveClaudeJsonPath(projectPath);
    const config = await readClaudeJson(configPath);
    if (config.mcpServers?.memory === undefined) {
        return configPath;
    }
    const { memory: _removed, ...remainingServers } = config.mcpServers;
    /* v8 ignore next */
    void _removed;
    const updated = {
        ...config,
        mcpServers: remainingServers,
    };
    await writeClaudeJson(configPath, updated);
    return configPath;
}
//# sourceMappingURL=memory-config-generator.js.map