"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, {
    IndexDict: function() {
        return IndexDict;
    },
    IndexList: function() {
        return IndexList;
    },
    IndexStructType: function() {
        return IndexStructType;
    },
    jsonToIndexStruct: function() {
        return jsonToIndexStruct;
    }
});
const _Node = require("../Node.js");
const _IndexStruct = require("./IndexStruct.js");
var IndexStructType;
(function(IndexStructType) {
    IndexStructType["SIMPLE_DICT"] = "simple_dict";
    IndexStructType["LIST"] = "list";
    IndexStructType["KEYWORD_TABLE"] = "keyword_table";
})(IndexStructType || (IndexStructType = {}));
class IndexDict extends _IndexStruct.IndexStruct {
    nodesDict = {};
    type = "simple_dict";
    getSummary() {
        if (this.summary === undefined) {
            throw new Error("summary field of the index dict is not set");
        }
        return this.summary;
    }
    addNode(node, textId) {
        const vectorId = textId ?? node.id_;
        this.nodesDict[vectorId] = node;
    }
    toJson() {
        const nodesDict = {};
        for (const [key, node] of Object.entries(this.nodesDict)){
            nodesDict[key] = node.toJSON();
        }
        return {
            ...super.toJson(),
            nodesDict,
            type: this.type
        };
    }
    delete(nodeId) {
        delete this.nodesDict[nodeId];
    }
}
function jsonToIndexStruct(json) {
    if (json.type === "list") {
        const indexList = new IndexList(json.indexId, json.summary);
        indexList.nodes = json.nodes;
        return indexList;
    } else if (json.type === "simple_dict") {
        const indexDict = new IndexDict(json.indexId, json.summary);
        indexDict.nodesDict = Object.entries(json.nodesDict).reduce((acc, [key, value])=>{
            acc[key] = (0, _Node.jsonToNode)(value);
            return acc;
        }, {});
        return indexDict;
    } else {
        throw new Error(`Unknown index struct type: ${json.type}`);
    }
}
class IndexList extends _IndexStruct.IndexStruct {
    nodes = [];
    type = "list";
    addNode(node) {
        this.nodes.push(node.id_);
    }
    toJson() {
        return {
            ...super.toJson(),
            nodes: this.nodes,
            type: this.type
        };
    }
}
