"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.structHashCrdt = void 0;
const insertion_1 = require("@jsonjoy.com/util/lib/sort/insertion");
const json_crdt_1 = require("../json-crdt");
const hash_1 = require("./hash");
const structHash_1 = require("./structHash");
/**
 * Constructs a structural hash of the view of the node.
 *
 * Produces a *structural hash* of a JSON CRDT node. Works the same as
 * `structHash, but uses the `JsonNode` interface instead of a generic value.
 *
 * @todo PERF: instead of constructing a "str" and "bin" view, iterate over
 *     the RGA chunks and hash them directly.
 */
const structHashCrdt = (node) => {
    if (node instanceof json_crdt_1.ConNode)
        return (0, structHash_1.structHash)(node.val);
    else if (node instanceof json_crdt_1.ValNode)
        return (0, exports.structHashCrdt)(node.node());
    else if (node instanceof json_crdt_1.StrNode)
        return (0, hash_1.hash)(node.view()).toString(36);
    else if (node instanceof json_crdt_1.ObjNode) {
        let res = '{';
        const keys = Array.from(node.keys.keys());
        (0, insertion_1.sort)(keys);
        const length = keys.length;
        for (let i = 0; i < length; i++) {
            const key = keys[i];
            const value = node.get(key);
            res += (0, hash_1.hash)(key).toString(36) + ':' + (0, exports.structHashCrdt)(value) + ',';
        }
        return res + '}';
    }
    else if (node instanceof json_crdt_1.ArrNode || node instanceof json_crdt_1.VecNode) {
        let res = '[';
        node.children((child) => {
            res += (0, exports.structHashCrdt)(child) + ';';
        });
        return res + ']';
    }
    else if (node instanceof json_crdt_1.BinNode)
        return (0, hash_1.hash)(node.view()).toString(36);
    return 'U';
};
exports.structHashCrdt = structHashCrdt;
//# sourceMappingURL=structHashCrdt.js.map