// Planu — engine/actuals/wip-tracker.ts (SPEC-061)
// Simple JSON file persistence for WIP actuals during implementation.
import * as fs from 'node:fs';
import * as path from 'node:path';
import { writeJsonSafe } from '../safety/atomic-writer.js';
/**
 * Returns the path to the WIP JSON file for a given spec.
 */
export function getWipPath(projectDataDir, specId) {
    return path.join(projectDataDir, 'actuals-wip', `${specId}.json`);
}
/**
 * Reads WIP actuals for a spec from disk.
 * Returns null if the file does not exist or cannot be parsed.
 */
export function readWip(projectDataDir, specId) {
    const filePath = getWipPath(projectDataDir, specId);
    try {
        const content = fs.readFileSync(filePath, { encoding: 'utf-8' });
        return JSON.parse(content);
    }
    catch {
        return null;
    }
}
/**
 * Writes WIP actuals for a spec to disk.
 * Creates the directory if it does not exist.
 */
export async function writeWip(projectDataDir, specId, wip) {
    const filePath = getWipPath(projectDataDir, specId);
    await writeJsonSafe(filePath, wip);
}
/**
 * Deletes the WIP actuals file for a spec if it exists.
 */
export function deleteWip(projectDataDir, specId) {
    const filePath = getWipPath(projectDataDir, specId);
    try {
        fs.unlinkSync(filePath);
    }
    catch {
        // File doesn't exist — nothing to do
    }
}
/**
 * Reads existing WIP, merges updates, and writes back.
 * If no existing WIP, creates a new one with defaults merged with updates.
 */
export async function updateWip(projectDataDir, specId, updates) {
    const existing = readWip(projectDataDir, specId);
    const now = new Date().toISOString();
    const base = existing ?? {
        specId,
        startedAt: now,
        tokensOpus: 0,
        tokensSonnet: 0,
        apiCostAccumulated: 0,
        lastUpdated: now,
        notes: '',
    };
    const merged = {
        ...base,
        ...updates,
        lastUpdated: now,
    };
    await writeWip(projectDataDir, specId, merged);
    return merged;
}
//# sourceMappingURL=wip-tracker.js.map