import { readJson, writeJson, hashProjectPath } from './base-store.js';
import { withFileLock } from './file-mutex.js';
function ideasFile(projectPath) {
    const hash = hashProjectPath(projectPath);
    return `data/projects/${hash}/backlog/ideas.json`;
}
/**
 * Save a new idea (appends to existing list).
 */
export async function saveIdea(projectPath, idea) {
    const file = ideasFile(projectPath);
    await withFileLock(file, async () => {
        const existing = await readJson(file, []);
        existing.push(idea);
        await writeJson(file, existing);
    });
}
/**
 * Load all ideas for a project.
 */
export async function loadIdeas(projectPath) {
    const file = ideasFile(projectPath);
    return readJson(file, []);
}
/**
 * Update a single idea by ID, applying partial patch.
 * Returns the updated idea, or null if not found.
 */
export async function updateIdea(projectPath, id, patch) {
    const file = ideasFile(projectPath);
    let result = null;
    await withFileLock(file, async () => {
        const existing = await readJson(file, []);
        const idx = existing.findIndex((i) => i.id === id);
        if (idx === -1) {
            return;
        }
        existing[idx] = { ...existing[idx], ...patch };
        await writeJson(file, existing);
        result = existing[idx] ?? null;
    });
    return result;
}
//# sourceMappingURL=ideas-store.js.map