"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toSchema = void 0;
const nodes_1 = require("../nodes");
const json_crdt_patch_1 = require("../../json-crdt-patch");
/**
 * Converts any JSON CRDT node to a schema representation. The schema can be
 * used to copy the structure of the JSON CRDT node to another document or
 * another location in the same document.
 *
 * @param node JSON CRDT node to recursively convert to schema.
 * @returns Schema representation of the JSON CRDT node.
 */
const toSchema = (node) => {
    if (node instanceof nodes_1.ConNode)
        return json_crdt_patch_1.s.con(node.val);
    if (node instanceof nodes_1.ValNode)
        return json_crdt_patch_1.s.val((0, exports.toSchema)(node.node()));
    if (node instanceof nodes_1.ObjNode) {
        const obj = {};
        node.nodes((child, key) => (obj[key] = (0, exports.toSchema)(child)));
        return json_crdt_patch_1.s.obj(obj);
    }
    if (node instanceof nodes_1.VecNode) {
        const arr = [];
        node.children((child) => arr.push((0, exports.toSchema)(child)));
        return json_crdt_patch_1.s.vec(...arr);
    }
    if (node instanceof nodes_1.StrNode)
        return json_crdt_patch_1.s.str(node.view());
    if (node instanceof nodes_1.BinNode)
        return json_crdt_patch_1.s.bin(node.view());
    if (node instanceof nodes_1.ArrNode) {
        const arr = [];
        node.children((child) => {
            if (child)
                arr.push((0, exports.toSchema)(child));
        });
        return json_crdt_patch_1.s.arr(arr);
    }
    return json_crdt_patch_1.s.con(undefined);
};
exports.toSchema = toSchema;
//# sourceMappingURL=toSchema.js.map