import { HolographicMemorySystem as NativeHMS } from '@hms/native';
import { getLogger } from '../../../core/logger.js';
import * as path from 'path';
const logger = getLogger('holographic-memory-system');
export class HolographicMemorySystem {
    constructor(config = {}) {
        this.memoryIndex = new Map();
        this.dimensions = config.dimensions || 10000;
        const storagePath = config.storagePath || path.join(process.cwd(), '.scrivener-hms.db');
        this.native = new NativeHMS(this.dimensions, storagePath);
        logger.info('HMS initialized', {
            dimensions: this.dimensions,
            persistence: storagePath,
        });
    }
    async memorizeText(text, id, traceId) {
        const memoryId = id || `text_${Date.now()}`;
        await this.native.memorizeText(memoryId, text, traceId);
        this.memoryIndex.set(memoryId, { modality: 'text', originalData: text });
        return { id: memoryId, modalities: ['text'] };
    }
    async memorizeDocument(document, traceId) {
        const memoryId = `doc_${document.id}`;
        await this.native.memorizeText(memoryId, document.content || '', traceId);
        this.memoryIndex.set(memoryId, { modality: 'document', originalData: document });
        return { id: memoryId, modalities: ['document'] };
    }
    async memorizeBatch(items, traceId) {
        await this.native.memorizeBatch(items, traceId);
        for (const item of items) {
            this.memoryIndex.set(item.id, { modality: 'text', originalData: item.text });
        }
    }
    async memorizeTextBuffer(id, buffer, traceId) {
        await this.native.memorizeTextBuffer(id, buffer, traceId);
        this.memoryIndex.set(id, { modality: 'text' });
        return { id, modalities: ['text'] };
    }
    async memorizeFile(id, filePath) {
        await this.native.memorizeFile(id, filePath);
        this.memoryIndex.set(id, { modality: 'file', originalData: filePath });
        return { id, modalities: ['file'] };
    }
    mapResults(results) {
        return results.map((r, index) => {
            const indexEntry = this.memoryIndex.get(r.id);
            return {
                id: r.id,
                similarity: r.similarity,
                entry: { id: r.id, metadata: indexEntry },
                rank: index + 1,
                reconstructed: indexEntry?.originalData,
                explanation: `Similarity: ${r.similarity.toFixed(3)} | Type: ${indexEntry?.modality || 'unknown'}`,
            };
        });
    }
    async queryText(text, k = 10, traceId) {
        const results = await this.native.query(text, k, traceId);
        return this.mapResults(results);
    }
    async findAnalogy(a, b, c, traceId) {
        const results = await this.native.findAnalogy(a, b, c, traceId);
        return this.mapResults(results);
    }
    async dream() {
        logger.info('HMS entering DREAM mode');
        try {
            const concepts = await this.native.synthesizeConcepts();
            return concepts.map((c) => ({
                conceptId: c.centroidId,
                thematicDensity: c.coherence,
                memberCount: c.memberCount,
                memberIds: c.memberIds,
                summary: `Concept: ${c.centroidId} (coherence: ${c.coherence.toFixed(3)}, members: ${c.memberCount})`,
            }));
        }
        catch (error) {
            logger.warn('HMS Dream mode failed', { error: error.message });
            return [];
        }
    }
    async discoverAssociations(id, threshold = 0.6) {
        const indexEntry = this.memoryIndex.get(id);
        if (!indexEntry?.originalData)
            return [];
        const text = typeof indexEntry.originalData === 'string'
            ? indexEntry.originalData
            : indexEntry.originalData.content || '';
        if (!text)
            return [];
        const results = await this.native.query(text, 20);
        return this.mapResults(results.filter((r) => r.id !== id && r.similarity >= threshold));
    }
    getStats() {
        return {
            dimensions: this.dimensions,
            engine: 'Rust Native v2.0',
            totalMemories: this.memoryIndex.size,
        };
    }
    async destroy() {
        logger.info('Shutting down HMS Engine');
        this.memoryIndex.clear();
    }
}
//# sourceMappingURL=holographic-memory-system.js.map