import { promises as fs, renameSync, mkdirSync, existsSync } from 'fs';
import { join } from 'path';
import { logger } from '../utils/logger.js';
import { summarizeError } from '../utils/error-display.js';
/**
 * PersistentStore — generic JSON file persistence with atomic writes.
 *
 * Handles lazy loading, atomic writes via a temporary file, and ensures the
 * directory hierarchy exists. Errors are logged but not re‑thrown so that the
 * caller can decide how to recover.
 */
export class PersistentStore {
    filePath;
    dir;
    constructor(filePath) {
        this.filePath = filePath;
        this.dir = join(filePath, '..');
    }
    /** Load JSON data from disk, or return null if the file does not exist or is invalid. */
    async load() {
        if (!existsSync(this.filePath)) {
            return null;
        }
        try {
            const raw = await fs.readFile(this.filePath, 'utf-8');
            return JSON.parse(raw);
        }
        catch (err) {
            logger.debug('PersistentStore: failed to load, starting fresh', { file: this.filePath, error: summarizeError(err) });
            return null;
        }
    }
    /** Atomically persist data to disk. */
    async persist(data) {
        try {
            await fs.mkdir(this.dir, { recursive: true });
            const tmpPath = `${this.filePath}.tmp`;
            const content = JSON.stringify(data, null, 2) + '\n';
            await fs.writeFile(tmpPath, content, 'utf-8');
            // renameSync is atomic on POSIX
            await fs.rename(tmpPath, this.filePath);
        }
        catch (err) {
            logger.debug('PersistentStore: persist failed (non-fatal)', { file: this.filePath, error: summarizeError(err) });
        }
    }
}
