// storage/decision-store.ts — JSON persistence for project decisions (SPEC-040)
// Persistence file: data/projects/{projectId}/decisions.json
import { readJson, writeJson, projectDataDir } from './base-store.js';
// ---------------------------------------------------------------------------
// File path
// ---------------------------------------------------------------------------
function decisionsFile(projectId) {
    return `${projectDataDir(projectId)}/decisions.json`;
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
async function loadData(projectId) {
    return readJson(decisionsFile(projectId), {
        decisions: [],
        lastUpdated: new Date().toISOString(),
    });
}
async function saveData(projectId, data) {
    await writeJson(decisionsFile(projectId), data);
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Get all decisions for a project. Returns an empty array when no data exists.
 */
export async function getDecisions(projectId) {
    const data = await loadData(projectId);
    return data.decisions;
}
/**
 * Persist a new decision. Does not check for duplicates — caller must ensure uniqueness.
 */
export async function saveDecision(projectId, decision) {
    const data = await loadData(projectId);
    data.decisions.push(decision);
    data.lastUpdated = new Date().toISOString();
    await saveData(projectId, data);
}
/**
 * Apply partial updates to an existing decision. Throws if not found.
 */
export async function updateDecision(projectId, id, updates) {
    const data = await loadData(projectId);
    const idx = data.decisions.findIndex((d) => d.id === id);
    if (idx === -1) {
        throw new Error(`Decision "${id}" not found in project "${projectId}".`);
    }
    const existing = data.decisions[idx];
    /* v8 ignore start */
    if (!existing) {
        throw new Error(`Decision "${id}" not found in project "${projectId}".`);
    }
    /* v8 ignore stop */
    const updated = { ...existing, ...updates, id };
    data.decisions[idx] = updated;
    data.lastUpdated = new Date().toISOString();
    await saveData(projectId, data);
    return updated;
}
/**
 * Get all decisions linked to a specific spec.
 */
export async function getDecisionsBySpec(projectId, specId) {
    const data = await loadData(projectId);
    return data.decisions.filter((d) => d.specId === specId);
}
/**
 * Get all decisions regardless of status or spec association.
 */
export async function getAllDecisions(projectId) {
    return getDecisions(projectId);
}
//# sourceMappingURL=decision-store.js.map