"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, {
    docToJson: function() {
        return docToJson;
    },
    isValidDocJson: function() {
        return isValidDocJson;
    },
    jsonToDoc: function() {
        return jsonToDoc;
    }
});
const _Node = require("../../Node.js");
const TYPE_KEY = "__type__";
const DATA_KEY = "__data__";
function isValidDocJson(docJson) {
    return typeof docJson === "object" && docJson !== null && docJson[TYPE_KEY] !== undefined && docJson[DATA_KEY] !== undefined;
}
function docToJson(doc) {
    return {
        [DATA_KEY]: JSON.stringify(doc.toJSON()),
        [TYPE_KEY]: doc.type
    };
}
function jsonToDoc(docDict) {
    const docType = docDict[TYPE_KEY];
    const dataDict = JSON.parse(docDict[DATA_KEY]);
    let doc;
    if (docType === _Node.ObjectType.DOCUMENT) {
        doc = new _Node.Document({
            text: dataDict.text,
            id_: dataDict.id_,
            embedding: dataDict.embedding,
            hash: dataDict.hash,
            metadata: dataDict.metadata
        });
    } else if (docType === _Node.ObjectType.TEXT) {
        doc = new _Node.TextNode({
            text: dataDict.text,
            id_: dataDict.id_,
            hash: dataDict.hash,
            metadata: dataDict.metadata
        });
    } else {
        throw new Error(`Unknown doc type: ${docType}`);
    }
    return doc;
}
