import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, unlinkSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { getMemoriesDir } from '../../../config.js';
import { DURABLE_MEMORY_INDEX } from './constants.js';
import { parseTopicDocument, renderTopicDocument } from './topic-file.js';
export class DurableMemoryWorkspace {
    rootDir;
    constructor(rootDir) {
        this.rootDir = rootDir ?? getMemoriesDir();
    }
    initialize() {
        mkdirSync(this.rootDir, { recursive: true });
    }
    getRootDir() {
        return this.rootDir;
    }
    getIndexPath() {
        return join(this.rootDir, DURABLE_MEMORY_INDEX);
    }
    getTopicPath(fileName) {
        return join(this.rootDir, fileName);
    }
    listTopicFiles() {
        this.initialize();
        return readdirSync(this.rootDir)
            .filter((file) => file.endsWith('.md') && file !== DURABLE_MEMORY_INDEX)
            .sort();
    }
    readTopicFile(fileName) {
        this.initialize();
        const topicPath = this.getTopicPath(fileName);
        if (!existsSync(topicPath))
            return null;
        return readFileSync(topicPath, 'utf-8');
    }
    writeTopicFile(fileName, content) {
        this.initialize();
        this.atomicWrite(this.getTopicPath(fileName), content);
    }
    deleteTopicFile(fileName) {
        this.initialize();
        const topicPath = this.getTopicPath(fileName);
        if (existsSync(topicPath)) {
            unlinkSync(topicPath);
        }
    }
    readTopicDocument(fileName) {
        const content = this.readTopicFile(fileName);
        if (!content)
            return null;
        return parseTopicDocument(fileName, content);
    }
    readAllTopicDocuments() {
        return this.listTopicFiles()
            .map((fileName) => this.readTopicDocument(fileName))
            .filter((topic) => topic !== null);
    }
    writeTopicDocument(topic) {
        this.writeTopicFile(topic.fileName, renderTopicDocument(topic));
    }
    readIndex() {
        this.initialize();
        const indexPath = this.getIndexPath();
        if (!existsSync(indexPath))
            return null;
        const content = readFileSync(indexPath, 'utf-8').trim();
        return content || null;
    }
    writeIndex(content) {
        this.initialize();
        this.atomicWrite(this.getIndexPath(), `${content.trim()}\n`);
    }
    deleteIndex() {
        this.initialize();
        const indexPath = this.getIndexPath();
        if (existsSync(indexPath)) {
            unlinkSync(indexPath);
        }
    }
    snapshot() {
        return {
            index: this.readIndex(),
            topics: this.listTopicFiles()
                .map((fileName) => {
                const content = this.readTopicFile(fileName);
                return content ? { fileName, content } : null;
            })
                .filter((topic) => topic !== null)
        };
    }
    restore(snapshot) {
        this.initialize();
        for (const fileName of this.listTopicFiles()) {
            this.deleteTopicFile(fileName);
        }
        this.deleteIndex();
        for (const topic of snapshot.topics) {
            this.writeTopicFile(topic.fileName, topic.content);
        }
        if (snapshot.index) {
            this.writeIndex(snapshot.index);
        }
    }
    clear() {
        rmSync(this.rootDir, { recursive: true, force: true });
    }
    atomicWrite(filePath, content) {
        const tempPath = `${filePath}.tmp`;
        writeFileSync(tempPath, content, 'utf-8');
        renameSync(tempPath, filePath);
    }
}
//# sourceMappingURL=workspace.js.map