var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { writeFileSync, existsSync, readFileSync } from 'fs';
import { LogClass } from './utils.js';
/**
 * This recorder will
 * - store auto snapshots in memory after every dispatch (for time travel debugging)
 * - persist a snapshot to disk when explicitly instructed to do so via createSnapshot()
 *
 */
let FileRecorder = class FileRecorder {
    snapshots = [];
    filePath;
    constructor(filePath) {
        this.filePath = filePath;
    }
    async afterDispatch(snapshot) {
        this.snapshots.push(snapshot);
    }
    async getSnapshots() {
        return this.snapshots;
    }
    async createSnapshot(snapshot) {
        // Look at snapshot.action.id if we want to save snapshots according to their id
        const data = JSON.stringify(snapshot, null, 2); // 2nd and 3rd args for pretty printing
        writeFileSync(this.filePath, data, 'utf8');
    }
    async loadSnapshot(id) {
        if (id) {
            // Check if we happen to have this in memory
            return this.snapshots.find((s) => s.action?.id === id) || null;
        }
        // else just fetch what we have from file
        try {
            if (existsSync(this.filePath)) {
                const data = readFileSync(this.filePath, 'utf8');
                return JSON.parse(data);
            }
            return null;
        }
        catch (err) {
            console.error('Error reading the snapshot file:', err);
            throw err;
        }
    }
};
FileRecorder = __decorate([
    LogClass()
], FileRecorder);
export { FileRecorder };
//# sourceMappingURL=FileRecorder.js.map