import { readJson, writeJson, projectDataDir } from '../base-store.js';
function knowledgeFile(projectId) {
    return `${projectDataDir(projectId)}/knowledge.json`;
}
/**
 * Get project knowledge. Returns `null` when no knowledge has been stored yet.
 */
export async function getKnowledge(projectId) {
    return readJson(knowledgeFile(projectId), null);
}
/**
 * Save or overwrite the full project knowledge object.
 */
export async function saveKnowledge(projectId, knowledge) {
    await writeJson(knowledgeFile(projectId), knowledge);
}
/**
 * Partially update project knowledge, merging with the existing object.
 */
export async function updateKnowledge(projectId, updates) {
    const existing = await getKnowledge(projectId);
    if (!existing) {
        throw new Error(`No knowledge found for project "${projectId}". Use saveKnowledge first.`);
    }
    const updated = {
        ...existing,
        ...updates,
        projectId, // never allow override
        lastAnalyzed: new Date().toISOString(),
    };
    await writeJson(knowledgeFile(projectId), updated);
    return updated;
}
/**
 * Delete all knowledge for a project. Returns `true` if it existed.
 */
export async function deleteKnowledge(projectId) {
    const existing = await getKnowledge(projectId);
    if (!existing) {
        return false;
    }
    // Overwrite with null sentinel so readJson returns null on next read
    await writeJson(knowledgeFile(projectId), null);
    return true;
}
export { knowledgeFile };
//# sourceMappingURL=knowledge.js.map