import * as crypto from 'crypto';
import { VectorStore } from '@langchain/core/vectorstores';
import { Document } from '@langchain/core/documents';
import { HolographicMemorySystem } from '../memory/hhm/holographic-memory-system.js';
/**
 * LangChain VectorStore implementation powered by the Rust-native HMS Engine
 */
export class LangChainHMSVectorStore extends VectorStore {
    _vectorstoreType() {
        return 'hms_native';
    }
    constructor(embeddings, args = {}) {
        super(embeddings, args);
        this.hms =
            args.hms ||
                new HolographicMemorySystem({
                    dimensions: args.dimensions,
                    storagePath: args.storagePath,
                });
    }
    async addVectors(_vectors, _documents) {
        // HMS handles its own encoding/vectorization from text
        // We use addDocuments instead to let the Rust engine do the work
        throw new Error('Use addDocuments instead of addVectors for HMSVectorStore to leverage native encoding.');
    }
    async addDocuments(documents) {
        const items = documents.map((doc) => ({
            id: (doc.metadata?.id || doc.metadata?.documentId || crypto.randomUUID()),
            text: doc.pageContent,
        }));
        await this.hms.memorizeBatch(items);
    }
    async similaritySearchVectorWithScore(_query, _k) {
        throw new Error('HMSVectorStore uses native text-based querying. Use similaritySearch instead.');
    }
    async similaritySearch(query, k = 4) {
        const results = await this.hms.queryText(query, k);
        return results.map((r) => new Document({
            pageContent: r.reconstructed || '',
            metadata: {
                id: r.id,
                similarity: r.similarity,
                ...(r.entry.metadata || {}),
            },
        }));
    }
    async similaritySearchWithScore(query, k = 4) {
        const results = await this.hms.queryText(query, k);
        return results.map((r) => [
            new Document({
                pageContent: r.reconstructed || '',
                metadata: {
                    id: r.id,
                    similarity: r.similarity,
                    ...(r.entry.metadata || {}),
                },
            }),
            r.similarity,
        ]);
    }
    static async fromDocuments(docs, embeddings, dbConfig) {
        const instance = new LangChainHMSVectorStore(embeddings, dbConfig);
        await instance.addDocuments(docs);
        return instance;
    }
    static async fromTexts(texts, metadatas, embeddings, dbConfig) {
        const docs = texts.map((text, i) => new Document({
            pageContent: text,
            metadata: Array.isArray(metadatas) ? metadatas[i] : metadatas,
        }));
        return LangChainHMSVectorStore.fromDocuments(docs, embeddings, dbConfig);
    }
}
//# sourceMappingURL=hms-vector-store.js.map