"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    IngestionCache: function() {
        return IngestionCache;
    },
    getTransformationHash: function() {
        return getTransformationHash;
    }
});
const _env = require("@llamaindex/env");
const _Node = require("../Node.js");
const _utils = require("../storage/docStore/utils.js");
const _SimpleKVStore = require("../storage/kvStore/SimpleKVStore.js");
const transformToJSON = (obj)=>{
    const seen = [];
    const replacer = (key, value)=>{
        if (value != null && typeof value == "object") {
            if (seen.indexOf(value) >= 0) {
                return;
            }
            seen.push(value);
        }
        return value;
    };
    // this is a custom replacer function that will allow us to handle circular references
    const jsonStr = JSON.stringify(obj, replacer);
    return jsonStr;
};
function getTransformationHash(nodes, transform) {
    const nodesStr = nodes.map((node)=>node.getContent(_Node.MetadataMode.ALL)).join("");
    const transformString = transformToJSON(transform);
    const hash = (0, _env.createSHA256)();
    hash.update(nodesStr + transformString);
    return hash.digest();
}
class IngestionCache {
    collection = "llama_cache";
    cache;
    nodesKey = "nodes";
    constructor(collection){
        if (collection) {
            this.collection = collection;
        }
        this.cache = new _SimpleKVStore.SimpleKVStore();
    }
    async put(hash, nodes) {
        const val = {
            [this.nodesKey]: nodes.map((node)=>(0, _utils.docToJson)(node))
        };
        await this.cache.put(hash, val, this.collection);
    }
    async get(hash) {
        const json = await this.cache.get(hash, this.collection);
        if (!json || !json[this.nodesKey] || !Array.isArray(json[this.nodesKey])) {
            return undefined;
        }
        return json[this.nodesKey].map((doc)=>(0, _utils.jsonToDoc)(doc));
    }
}
