// storage/approval-store.ts — Approval workflow persistence (SPEC-283)
// ApprovalRecord[] stored in data/projects/{hash}/approvals/{specId}.json
// ApprovalPolicy stored in data/projects/{hash}/approval-policy.json
import { join } from 'node:path';
import { readJson, writeJson, projectDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// ---------------------------------------------------------------------------
// Path helpers
// ---------------------------------------------------------------------------
function approvalRecordsPath(projectId, specId) {
    return join(projectDataDir(projectId), 'approvals', `${specId}.json`);
}
function approvalPolicyPath(projectId) {
    return join(projectDataDir(projectId), 'approval-policy.json');
}
// ---------------------------------------------------------------------------
// Policy CRUD
// ---------------------------------------------------------------------------
/** Read the approval policy for a project, or null if not configured. */
export async function getPolicy(projectId) {
    const raw = await readJson(approvalPolicyPath(projectId), null);
    return raw;
}
/** Persist the approval policy for a project. */
export async function savePolicy(projectId, policy) {
    await writeJson(approvalPolicyPath(projectId), policy);
    return policy;
}
// ---------------------------------------------------------------------------
// Records CRUD
// ---------------------------------------------------------------------------
/** Read all approval records for a given spec. */
export async function getRecords(projectId, specId) {
    return readJson(approvalRecordsPath(projectId, specId), []);
}
/** Append a new approval record. Returns the full updated list. */
export async function appendRecord(projectId, specId, record) {
    const filePath = approvalRecordsPath(projectId, specId);
    return withFileLock(filePath, async () => {
        const existing = await readJson(filePath, []);
        const updated = [...existing, record];
        await writeJson(filePath, updated);
        return updated;
    });
}
/** Delete all approval records for a spec (e.g. when changes are requested). */
export async function clearRecords(projectId, specId) {
    await writeJson(approvalRecordsPath(projectId, specId), []);
}
//# sourceMappingURL=approval-store.js.map